Quantcast
Channel: OpenCV Q&A Forum - RSS feed
Viewing all articles
Browse latest Browse all 41027

How to save video using thread

$
0
0
I am making a program that takes a video for 10 seconds when an object is detected.
If 'out' is created before 'while', the video is saved only once
when use this code .avi file is just created and there is nothing saved
Is it the right way to use threads? Or is there any other way? import cv2 import numpy as np import time import threading def thread(): global recording,out recording = True print("recording start") time.sleep(10) recording = False out.release() print("recording end") global recording, out cap = cv2.VideoCapture("rtsp://128.1.1.6/profile2/media.smp") fgbg = cv2.createBackgroundSubtractorMOG2(varThreshold=200, detectShadows=0) fps = 60 width = int(cap.get(3)) height = int(cap.get(4)) fcc = cv2.VideoWriter_fourcc(*'XVID') recording = False while(1): ret, frame = cap.read() hms = time.strftime('%H:%M:%S', time.localtime()) hmss = time.strftime('%H_%M_%S', time.localtime()) fgmask = fgbg.apply(frame) nlabels, labels, stats, centroids = cv2.connectedComponentsWithStats(fgmask) cv2.putText(frame,str(hms),(0,15),cv2.FONT_HERSHEY_SIMPLEX,0.5,(0,255,255)) for index, centroid in enumerate(centroids): if stats[index][0] == 0 and stats[index][1] == 0: continue if np.any(np.isnan(centroid)): continue x, y, width, height, area = stats[index] centerX, centerY = int(centroid[0]), int(centroid[1]) if area > 50: #Minimum size detected cv2.circle(frame, (centerX, centerY), 1, (0, 255, 0), 2) cv2.rectangle(frame, (x, y), (x + width, y + height), (0, 0, 255)) cv2.putText(frame, str(area), (centerX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 255)) # detected size if area > 1000 and not recording: t1 = threading.Thread(target=thread) path = 'E:\\test_' + str(hmss) + '.avi' out = cv2.VideoWriter(path, fcc, fps, (width, height)) recording = True print(str(hms)) t1.start() cv2.imshow('mask', fgmask) cv2.imshow('frame', frame) if recording: out.write(frame) k = cv2.waitKey(1) & 0xff cap.release() out.release() cv2.destroyAllWindows()

Viewing all articles
Browse latest Browse all 41027


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>