Hi,
I have been trying to perform object tracking in python using OpenCV. I’m able to extract the images from the video and trying to track the object from the same video. When I’m running my jupyter notebook cell it can read the video, but the problem lies that I’m not able to view the video on my machine. There is no pop-up of the video. Is something I’m doing wrong?
class Recognize(object):
def __init__(self):
self.capture = cv2.VideoCapture('video.mp4')
print(self.capture.isOpened()) # returns True
self.templates = []
for file in os.listdir(os.getcwd()):
filename = os.fsdecode(file)
if filename.endswith(".jpg") or filename.endswith(".png"):
self.templates.append(filename)
self.template_list = []
self.templ_shapes = []
self.names = []
for i, list_name in zip(range(len(self.templates)), self.templates):
self.names.append(list_name.split('.')[0])
self.template_list.append(cv2.imread(list_name, cv2.IMREAD_GRAYSCALE))
self.templ_shapes.append(self.template_list[i].shape[:: -1])
methods = [cv2.TM_CCOEFF, cv2.TM_CCOEFF_NORMED, cv2.TM_CCORR,
cv2.TM_CCORR_NORMED, cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]
thres = [0.35, 0.7, 0.65, 0.92, 0.75, 0.92]
i = 0
self.selected_method = methods[i]
self.thresh = thres[i]
self.main()
def main(self):
# Read frame
(self.status, self.frame) = self.capture.read()
seconds = 1
fps = self.capture.get(cv2.CAP_PROP_FPS)
multiplier = fps * seconds
while self.status:
frameId = int(round(self.capture.get(1)))
(self.status, self.frame) = self.capture.read()
if frameId % multiplier == 0:
gray_frame = cv2.cvtColor(self.frame, cv2.COLOR_BGR2GRAY)
for temp, template, shape, name in zip(self.templates, self.template_list, self.templ_shapes, self.names):
w, h = shape
res = cv2.matchTemplate(gray_frame, template, self.selected_method)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
if self.selected_method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]:
top_left = min_loc
if self.selected_method == cv2.TM_SQDIFF:
value = min_val
else:
value = 1 - min_val
else:
top_left = max_loc
value = max_val
bottom_right = (top_left[0] + w, top_left[1] + h)
if self.selected_method in [cv2.TM_CCOEFF, cv2.TM_SQDIFF]:
value = value / (10**8)
if self.selected_method == cv2.TM_SQDIFF:
value = 1 - value
elif self.selected_method == cv2.TM_CCORR:
value = value / (10**9)
rectangle_bgr = (255, 255, 255)
font = cv2.FONT_HERSHEY_PLAIN
font_scale = 1.5
(text_width, text_height) = cv2.getTextSize(f'{name}', font, fontScale=font_scale, thickness=1)[0]
if (value > self.thresh):
cv2.rectangle(self.frame, top_left, bottom_right, 255, 1)
box_coords = ((top_left[0], top_left[1]-10), (top_left[0] + text_width + 5, (top_left[1]-10) - text_height - 5))
cv2.rectangle(self.frame, box_coords[0], box_coords[1], rectangle_bgr, cv2.FILLED)
cv2.putText(self.frame, f'{name}', (top_left[0],top_left[1]-10), font, font_scale, (0,0,0), 2)
cv2.imshow('Object Recognition', self.frame)
key = cv2.waitKey(1)
if key == ord('q'):
break
self.capture.release()
cv2.destroyAllWindows()