Skip to content

Commit 1a8a04e

Browse files
committed
Optimize rendering speed of steering and car video
Fixes akshaybahadur21#15 Optimize the rendering speed of both the steering and car video in `Autopilot_V2/AutopilotApp_V2.py` and `Autopilot/DriveApp.py`. * **Autopilot_V2/AutopilotApp_V2.py** - Import `time` module. - Optimize `cv2.resize` function by using `cv2.INTER_LINEAR` interpolation method. - Set `cv2.CAP_PROP_FPS` property to 60 for `cv2.VideoCapture`. - Add `cv2.WINDOW_NORMAL` flag to `cv2.namedWindow` for 'frame' and 'steering wheel'. - Add a timer to measure the time taken for each frame processing and display the FPS. * **Autopilot/DriveApp.py** - Import `time` module. - Optimize `cv2.resize` function by using `cv2.INTER_LINEAR` interpolation method. - Set `cv2.CAP_PROP_FPS` property to 60 for `cv2.VideoCapture`. - Add `cv2.WINDOW_NORMAL` flag to `cv2.namedWindow` for 'frame' and 'steering wheel'. - Add a timer to measure the time taken for each frame processing and display the FPS.
1 parent 1bdae68 commit 1a8a04e

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

Autopilot/DriveApp.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import numpy as np
22
import cv2
33
from keras.models import load_model
4+
import time
45

56
model = load_model('models/Autopilot.h5')
67

@@ -14,7 +15,7 @@ def keras_predict(model, image):
1415
def keras_process_image(img):
1516
image_x = 40
1617
image_y = 40
17-
img = cv2.resize(img, (image_x, image_y))
18+
img = cv2.resize(img, (image_x, image_y), interpolation=cv2.INTER_LINEAR)
1819
img = np.array(img, dtype=np.float32)
1920
img = np.reshape(img, (-1, image_x, image_y, 1))
2021
return img
@@ -25,18 +26,25 @@ def keras_process_image(img):
2526
smoothed_angle = 0
2627

2728
cap = cv2.VideoCapture('resources/run.mp4')
29+
cap.set(cv2.CAP_PROP_FPS, 60)
30+
cv2.namedWindow('frame', cv2.WINDOW_NORMAL)
31+
cv2.namedWindow('steering wheel', cv2.WINDOW_NORMAL)
2832
while (cap.isOpened()):
33+
start_time = time.time()
2934
ret, frame = cap.read()
30-
gray = cv2.resize((cv2.cvtColor(frame, cv2.COLOR_RGB2HSV))[:, :, 1], (40, 40))
35+
gray = cv2.resize((cv2.cvtColor(frame, cv2.COLOR_RGB2HSV))[:, :, 1], (40, 40), interpolation=cv2.INTER_LINEAR)
3136
steering_angle = keras_predict(model, gray)
3237
print(steering_angle)
33-
cv2.imshow('frame', cv2.resize(frame, (500, 300), interpolation=cv2.INTER_AREA))
38+
cv2.imshow('frame', cv2.resize(frame, (500, 300), interpolation=cv2.INTER_LINEAR))
3439
smoothed_angle += 0.2 * pow(abs((steering_angle - smoothed_angle)), 2.0 / 3.0) * (
3540
steering_angle - smoothed_angle) / abs(
3641
steering_angle - smoothed_angle)
3742
M = cv2.getRotationMatrix2D((cols / 2, rows / 2), -smoothed_angle, 1)
3843
dst = cv2.warpAffine(steer, M, (cols, rows))
3944
cv2.imshow("steering wheel", dst)
45+
end_time = time.time()
46+
fps = 1 / (end_time - start_time)
47+
print("FPS: ", fps)
4048

4149
if cv2.waitKey(1) & 0xFF == ord('q'):
4250
break

Autopilot_V2/AutopilotApp_V2.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import numpy as np
22
import cv2
33
from keras.models import load_model
4+
import time
45

56
model = load_model('models/Autopilot.h5')
67

@@ -14,7 +15,7 @@ def keras_predict(model, image):
1415
def keras_process_image(img):
1516
image_x = 100
1617
image_y = 100
17-
img = cv2.resize(img, (image_x, image_y))
18+
img = cv2.resize(img, (image_x, image_y), interpolation=cv2.INTER_LINEAR)
1819
img = np.array(img, dtype=np.float32)
1920
img = np.reshape(img, (-1, image_x, image_y, 1))
2021
return img
@@ -25,18 +26,25 @@ def keras_process_image(img):
2526
smoothed_angle = 0
2627

2728
cap = cv2.VideoCapture('run.mp4')
29+
cap.set(cv2.CAP_PROP_FPS, 60)
30+
cv2.namedWindow('frame', cv2.WINDOW_NORMAL)
31+
cv2.namedWindow('steering wheel', cv2.WINDOW_NORMAL)
2832
while (cap.isOpened()):
33+
start_time = time.time()
2934
ret, frame = cap.read()
30-
gray = cv2.resize((cv2.cvtColor(frame, cv2.COLOR_RGB2HSV))[:, :, 1], (100, 100))
35+
gray = cv2.resize((cv2.cvtColor(frame, cv2.COLOR_RGB2HSV))[:, :, 1], (100, 100), interpolation=cv2.INTER_LINEAR)
3136
steering_angle = keras_predict(model, gray)
3237
print(steering_angle)
33-
cv2.imshow('frame', cv2.resize(frame, (600, 400), interpolation=cv2.INTER_AREA))
38+
cv2.imshow('frame', cv2.resize(frame, (600, 400), interpolation=cv2.INTER_LINEAR))
3439
smoothed_angle += 0.2 * pow(abs((steering_angle - smoothed_angle)), 2.0 / 3.0) * (
3540
steering_angle - smoothed_angle) / abs(
3641
steering_angle - smoothed_angle)
3742
M = cv2.getRotationMatrix2D((cols / 2, rows / 2), -smoothed_angle, 1)
3843
dst = cv2.warpAffine(steer, M, (cols, rows))
3944
cv2.imshow("steering wheel", dst)
45+
end_time = time.time()
46+
fps = 1 / (end_time - start_time)
47+
print("FPS: ", fps)
4048

4149
if cv2.waitKey(1) & 0xFF == ord('q'):
4250
break

0 commit comments

Comments
 (0)