im having problems doing this: I have a video, I want to read it and draw circles on it in realtime. I have three lists with x,y coordinates for the circles, and times in second.
x = ['20' , '30' , '40', '50','60' , '70' , '80', '90','100' , '110' ]
y = ['20' , '30' , '40', '50','60' , '70' , '80', '90','100' , '110' ]
times = [1,2,3,4,5,6,7,8,9,10]#Seconds
I want to draw on the video each coordinates associate with each second, so
One second, Draw a circle with 20(x) and 20(y)
Two second, Draw a circle with 30(x) and 30(y)
Three second, Draw a circle with 40(x) and 40(y)...
and so on.
I have tried something but im really bad
import cv2
import numpy as np
import time
a = ['20' , '30' , '40', '50','60' , '70' , '80', '90','100' , '110' ]
b = ['20' , '30' , '40', '50','60' , '70' , '80', '90','100' , '110' ]
time = [1,2,3,4,5,6,7,8,9,10]#Seconds
#ceate a capture object-------------------------------------------------------------------
cap=cv2.VideoCapture(r'C:/Users/aless/Documents/GitHub/Tobii-Glasses-Thesis/video/scenevideo5.mp4')
i=0
while(cap.isOpened()):
ret, frame = cap.read()
time_passed = int(cap.get(cv2.CAP_PROP_POS_MSEC))
if time_passed % (time[i]*1000) and i<=(len(time)-1):
print(time_passed)
# draw circles
cv2.circle(frame, (int(a[i]),int(b[i])), 10, (255, 0, 0), -1)
cv2.imshow('test', frame) # draw
i =1
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
After drawed first circle it don't draw the other. Can someone help? :C
EDIT 1: I tried in this way, but it gives me error
error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
import cv2
import numpy as np
import time
a = ['20' , '30' , '40', '50','60' , '70' , '80', '90','100' , '110' ]
b = ['20' , '30' , '40', '50','60' , '70' , '80', '90','100' , '110' ]
times = [1,2,3,4,5,6,7,8,9,10]#Seconds
#ceate a capture object-------------------------------------------------------------------
cap=cv2.VideoCapture(r'C:/Users/aless/Documents/GitHub/Tobii-Glasses-Thesis/video/scenevideo5.mp4')
count = 0
success = True
fps = int(cap.get(cv2.CAP_PROP_FPS))
i=0
while(cap.isOpened()):
ret, frame = cap.read()
time_passed = int(cap.get(cv2.CAP_PROP_POS_MSEC))
for x,y,t in zip(a,b,times):
if count%(t*fps) == 0 :
# draw circles
cv2.circle(frame, (int(x),int(x)), 10, (255, 0, 0), -1)
cv2.imshow('test', frame) # draw
count =1
cap.release()
cv2.destroyAllWindows()
CodePudding user response:
There are two main issues:
count = 1
should be outside thefor
loop.The condition
count % (t*fps) == 0
is incorrect.
You have probably meant it to beif (count - (t*fps)) == 0
In casefps
is not an integer, we may also check:if abs(count - (t*fps)) <= 0.001
In casefps
is an integer we can simply check:if count == (t*fps)
Debugging the code:
Instead of working on some arbitrary video file, it's simpler to debug the code using synthetic video file.
The following code creates a synthetic video, with running frame counter:
vid_filename = 'synthetic_video.mp4'
width, height, fps, n_frames = 320, 240, 25, 300
synthetic_out = cv2.VideoWriter(vid_filename, cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height))
for i in range(n_frames):
img = np.full((height, width, 3), 60, np.uint8)
cv2.putText(img, str(i 1), (width//2-40*len(str(i 1)), height//2 40), cv2.FONT_HERSHEY_DUPLEX, 4, (30, 255, 30), 8) # Green number
synthetic_out.write(img)
synthetic_out.release()
For testing, add cv2.waitKey(1000)
after cv2.imshow('test', frame)
.
Waiting a full second allows us to see if the circle is drawn on the correct frame.
Place a breakpoint, start debugging and place relevant variables in the watch window.
It also helps executing the code step by step...
Complete code sample:
import numpy as np
import cv2
import os.path
# Build synthetic video for testing (320x240 at 25fps and 300 frames)
################################################################################
vid_filename = 'synthetic_video.mp4'
if not os.path.isfile(vid_filename): # Create the file only once (if not exists).
width, height, fps, n_frames = 320, 240, 25, 300
synthetic_out = cv2.VideoWriter(vid_filename, cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height))
for i in range(n_frames):
img = np.full((height, width, 3), 60, np.uint8)
cv2.putText(img, str(i 1), (width//2-40*len(str(i 1)), height//2 40), cv2.FONT_HERSHEY_DUPLEX, 4, (30, 255, 30), 8) # Green number
synthetic_out.write(img)
synthetic_out.release()
################################################################################
a = ['20', '30', '40', '50', '60', '70', '80', '90', '100', '110']
b = ['20', '30', '40', '50', '60', '70', '80', '90', '100', '110']
times = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # Seconds
# create a capture object-------------------------------------------------------------------
cap = cv2.VideoCapture(vid_filename)
count = 1 # Start counting from 1 - assume first frame is 1 and not zero (draw a circle on frames 25, 50, 75 and not 26, 51, 76...)
fps = int(cap.get(cv2.CAP_PROP_FPS))
i = 0
while cap.isOpened():
ret, frame = cap.read()
if ret is False:
break # Break loop in case ret is false (should be false after the last frame)
for x, y, t in zip(a, b, times):
if count == (t*fps):
# draw circles
cv2.circle(frame, (int(x), int(y)), 10, (255, 0, 0), -1)
cv2.imshow('test', frame)
cv2.waitKey(1000)
count = 1
cap.release()
cv2.destroyAllWindows()
Output samples: