Skip to content

Commit 7ad100d

Browse files
authored
Merge pull request #11 from pitfisher/fps_visualization
Fps visualization
2 parents 33c26c3 + 983f0ff commit 7ad100d

File tree

6 files changed

+206
-16
lines changed

6 files changed

+206
-16
lines changed

src/camera-test/cv_camera_test.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,64 @@
1111
# ------------------------------------------------------------------------------
1212

1313
import cv2
14+
import time
15+
import numpy as np
1416

1517
CAMERA_DEVICE_ID = 0
1618
IMAGE_WIDTH = 320
1719
IMAGE_HEIGHT = 240
20+
fps = 0
21+
22+
23+
def visualize_fps(image, fps: int):
24+
if len(np.shape(image)) < 3:
25+
text_color = (255, 255, 255) # white
26+
else:
27+
text_color = (0, 255, 0) # green
28+
row_size = 20 # pixels
29+
left_margin = 24 # pixels
30+
31+
font_size = 1
32+
font_thickness = 1
33+
34+
# Draw the FPS counter
35+
fps_text = 'FPS = {:.1f}'.format(fps)
36+
text_location = (left_margin, row_size)
37+
cv2.putText(image, fps_text, text_location, cv2.FONT_HERSHEY_PLAIN,
38+
font_size, text_color, font_thickness)
39+
40+
return image
41+
1842

1943
if __name__ == "__main__":
2044
try:
2145
# create video capture
2246
cap = cv2.VideoCapture(CAMERA_DEVICE_ID)
2347

24-
# set resolution to 320x240 to reduce latency
48+
# set resolution to 320x240 to reduce latency
2549
cap.set(3, IMAGE_WIDTH)
2650
cap.set(4, IMAGE_HEIGHT)
2751

2852
# Loop to continuously get images
2953
while True:
54+
# ----------------------------------------------------------------------
55+
# record start time
56+
start_time = time.time()
57+
3058
# Read the frames from a camera
3159
_, frame = cap.read()
3260

3361
# show image
34-
cv2.imshow('frame', frame)
62+
cv2.imshow('frame', visualize_fps(frame, fps))
63+
64+
# ----------------------------------------------------------------------
65+
# record end time
66+
end_time = time.time()
67+
68+
# calculate FPS
69+
seconds = end_time - start_time
70+
fps = 1.0 / seconds
71+
print("Estimated fps:{0:0.1f}".format(fps))
3572

3673
# if key pressed is 'Esc' then exit the loop
3774
if cv2.waitKey(33) == 27:

src/face-detection/face-detection.py

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import cv2
2+
import numpy as np
3+
import time
24

35
# ------------------------------------------------------------------------------
46
# automaticdai
@@ -7,19 +9,45 @@
79
# Twitter: @yfrobotics
810
# Website: https://www.yfrl.org
911
# ------------------------------------------------------------------------------
10-
# Reference:
12+
# Reference:
1113
# - https://towardsdatascience.com/face-detection-in-2-minutes-using-opencv-python-90f89d7c0f81
1214
# ------------------------------------------------------------------------------
1315

16+
fps = 0
17+
18+
19+
def visualize_fps(image, fps: int):
20+
if len(np.shape(image)) < 3:
21+
text_color = (255, 255, 255) # white
22+
else:
23+
text_color = (0, 255, 0) # green
24+
row_size = 20 # pixels
25+
left_margin = 24 # pixels
26+
27+
font_size = 1
28+
font_thickness = 1
29+
30+
# Draw the FPS counter
31+
fps_text = 'FPS = {:.1f}'.format(fps)
32+
text_location = (left_margin, row_size)
33+
cv2.putText(image, fps_text, text_location, cv2.FONT_HERSHEY_PLAIN,
34+
font_size, text_color, font_thickness)
35+
36+
return image
37+
38+
1439
# Load the cascade
1540
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
1641

17-
# To capture video from webcam.
42+
# To capture video from webcam.
1843
cap = cv2.VideoCapture(0)
19-
# To use a video file as input
44+
# To use a video file as input
2045
# cap = cv2.VideoCapture('filename.mp4')
2146

2247
while True:
48+
# ----------------------------------------------------------------------
49+
# record start time
50+
start_time = time.time()
2351
# Read the frame
2452
_, img = cap.read()
2553
# Convert to grayscale
@@ -30,7 +58,14 @@
3058
for (x, y, w, h) in faces:
3159
cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
3260
# Display
33-
cv2.imshow('img', img)
61+
cv2.imshow('img', visualize_fps(img, fps))
62+
# ----------------------------------------------------------------------
63+
# record end time
64+
end_time = time.time()
65+
# calculate FPS
66+
seconds = end_time - start_time
67+
fps = 1.0 / seconds
68+
print("Estimated fps:{0:0.1f}".format(fps))
3469
# Stop if escape key is pressed
3570
k = cv2.waitKey(30) & 0xff
3671
if k==27:

src/motion-detection/cv_motion_detection.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
MOTION_BLUR = True
2121

2222
cnt_frame = 0
23+
fps = 0
2324

2425

2526
def mse(image_a, image_b):
@@ -34,6 +35,26 @@ def mse(image_a, image_b):
3435
return err
3536

3637

38+
def visualize_fps(image, fps: int):
39+
if len(np.shape(image)) < 3:
40+
text_color = (255, 255, 255) # white
41+
else:
42+
text_color = (0, 255, 0) # green
43+
row_size = 20 # pixels
44+
left_margin = 24 # pixels
45+
46+
font_size = 1
47+
font_thickness = 1
48+
49+
# Draw the FPS counter
50+
fps_text = 'FPS = {:.1f}'.format(fps)
51+
text_location = (left_margin, row_size)
52+
cv2.putText(image, fps_text, text_location, cv2.FONT_HERSHEY_PLAIN,
53+
font_size, text_color, font_thickness)
54+
55+
return image
56+
57+
3758
if __name__ == "__main__":
3859
try:
3960
# create video capture
@@ -64,8 +85,8 @@ def mse(image_a, image_b):
6485
edges = cv2.Canny(frame_gray,100,200)
6586

6687
# Show the original and processed image
67-
cv2.imshow('gray', frame_gray)
68-
cv2.imshow('edge', edges)
88+
cv2.imshow('gray', visualize_fps(frame_gray, fps))
89+
cv2.imshow('edge', visualize_fps(edges, fps))
6990

7091
# Calculate MSE
7192
if cnt_frame > 0:

src/object-tracking-color/cv_object_tracking_color.py

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,19 @@
2020

2121
import cv2
2222
import numpy as np
23+
import time
2324

2425
CAMERA_DEVICE_ID = 0
2526
IMAGE_WIDTH = 320
2627
IMAGE_HEIGHT = 240
28+
fps = 0
2729

2830
hsv_min = np.array((50, 80, 80))
2931
hsv_max = np.array((120, 255, 255))
3032

3133
colors = []
3234

35+
3336
def isset(v):
3437
try:
3538
type (eval(v))
@@ -109,6 +112,26 @@ def rgb2hsv(r, g, b):
109112
return (h, s, v)
110113

111114

115+
def visualize_fps(image, fps: int):
116+
if len(np.shape(image)) < 3:
117+
text_color = (255, 255, 255) # white
118+
else:
119+
text_color = (0, 255, 0) # green
120+
row_size = 20 # pixels
121+
left_margin = 24 # pixels
122+
123+
font_size = 1
124+
font_thickness = 1
125+
126+
# Draw the FPS counter
127+
fps_text = 'FPS = {:.1f}'.format(fps)
128+
text_location = (left_margin, row_size)
129+
cv2.putText(image, fps_text, text_location, cv2.FONT_HERSHEY_PLAIN,
130+
font_size, text_color, font_thickness)
131+
132+
return image
133+
134+
112135
if __name__ == "__main__":
113136
try:
114137
# create video capture
@@ -119,6 +142,9 @@ def rgb2hsv(r, g, b):
119142
cap.set(4, IMAGE_HEIGHT)
120143

121144
while True:
145+
# ----------------------------------------------------------------------
146+
# record start time
147+
start_time = time.time()
122148
# Read the frames frome a camera
123149
_, frame = cap.read()
124150
frame = cv2.blur(frame,(3,3))
@@ -128,6 +154,7 @@ def rgb2hsv(r, g, b):
128154

129155
# Convert the image to hsv space and find range of colors
130156
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
157+
cv2.namedWindow('frame')
131158
cv2.setMouseCallback('frame', on_mouse_click, frame)
132159

133160
# Uncomment this for RED tag
@@ -179,9 +206,15 @@ def rgb2hsv(r, g, b):
179206

180207
# Show the original and processed image
181208
#res = cv2.bitwise_and(frame, frame, mask=thresh2)
182-
cv2.imshow('frame', frame)
183-
cv2.imshow('thresh', thresh2)
184-
209+
cv2.imshow('frame', visualize_fps(frame, fps))
210+
cv2.imshow('thresh', visualize_fps(thresh2, fps))
211+
# ----------------------------------------------------------------------
212+
# record end time
213+
end_time = time.time()
214+
# calculate FPS
215+
seconds = end_time - start_time
216+
fps = 1.0 / seconds
217+
print("Estimated fps:{0:0.1f}".format(fps));
185218
# if key pressed is 'Esc' then exit the loop
186219
if cv2.waitKey(33) == 27:
187220
break

src/object-tracking-feature/orb.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import cv2
1616
import numpy as np
17-
17+
import time
1818

1919
def ORB_detector(new_image, image_template):
2020
# Function that compares input image to template
@@ -41,12 +41,38 @@ def ORB_detector(new_image, image_template):
4141
matches = sorted(matches, key=lambda val: val.distance)
4242
return len(matches)
4343

44+
45+
def visualize_fps(image, fps: int):
46+
if len(np.shape(image)) < 3:
47+
text_color = (255, 255, 255) # white
48+
else:
49+
text_color = (0, 255, 0) # green
50+
row_size = 20 # pixels
51+
left_margin = 24 # pixels
52+
53+
font_size = 1
54+
font_thickness = 1
55+
56+
# Draw the FPS counter
57+
fps_text = 'FPS = {:.1f}'.format(fps)
58+
text_location = (left_margin, row_size)
59+
cv2.putText(image, fps_text, text_location, cv2.FONT_HERSHEY_PLAIN,
60+
font_size, text_color, font_thickness)
61+
62+
return image
63+
64+
65+
fps = 0
66+
4467
cap = cv2.VideoCapture(0)
4568

4669
# Load our image template, this is our reference image
4770
image_template = cv2.imread('simple.png', 0)
4871

4972
while True:
73+
# ----------------------------------------------------------------------
74+
# record start time
75+
start_time = time.time()
5076
# Get webcam images
5177
ret, frame = cap.read()
5278

@@ -85,7 +111,14 @@ def ORB_detector(new_image, image_template):
85111
cv2.rectangle(frame, (top_left_x,top_left_y), (bottom_right_x,bottom_right_y), (0,255,0), 3)
86112
cv2.putText(frame,'Object Found',(50,50), cv2.FONT_HERSHEY_COMPLEX, 2 ,(0,255,0), 2)
87113

88-
cv2.imshow('Object Detector using ORB', frame)
114+
cv2.imshow('Object Detector using ORB', visualize_fps(frame, fps))
115+
# ----------------------------------------------------------------------
116+
# record end time
117+
end_time = time.time()
118+
# calculate FPS
119+
seconds = end_time - start_time
120+
fps = 1.0 / seconds
121+
print("Estimated fps:{0:0.1f}".format(fps))
89122
if cv2.waitKey(1) == 13: #13 is the Enter Key
90123
break
91124

0 commit comments

Comments
 (0)