/ recorded vedio.py
recorded vedio.py
1 import cv2 2 import cvzone 3 from sms import send_msg 4 5 thres = 0.55 6 nmsThres = 0.2 7 8 # Path to your video file 9 video_path = r"YOUR_PATH" 10 11 classNames = [] 12 classFile = 'coco.names' 13 with open(classFile, 'rt') as f: 14 classNames = f.read().split('\n') 15 16 configPath = 'ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt' 17 weightsPath = "frozen_inference_graph.pb" 18 19 net = cv2.dnn_DetectionModel(weightsPath, configPath) 20 net.setInputSize(320, 320) 21 net.setInputScale(1.0 / 127.5) 22 net.setInputMean((127.5, 127.5, 127.5)) 23 net.setInputSwapRB(True) 24 25 # Open the video file 26 cap = cv2.VideoCapture(video_path) 27 28 # Get the video's frame width and height 29 frame_width = int(cap.get(3)) 30 frame_height = int(cap.get(4)) 31 32 # Specify the codec and create VideoWriter object 33 out = cv2.VideoWriter('output.avi', cv2.VideoWriter_fourcc(*'MJPG'), 10, (frame_width, frame_height)) 34 35 sms_sent = False # Flag variable to track SMS sent status 36 37 while cap.isOpened(): 38 ret, frame = cap.read() 39 if not ret: 40 break 41 42 classIds, confs, bbox = net.detect(frame, confThreshold=thres, nmsThreshold=nmsThres) 43 44 # Send SMS only once for the first frame 45 if not sms_sent: 46 try: 47 send_msg() 48 sms_sent = True # Set flag to True once SMS is sent 49 except Exception as e: 50 print("Error sending SMS:", e) 51 52 if len(classIds) != 0: 53 for classId, conf, box in zip(classIds.flatten(), confs.flatten(), bbox): 54 if classId == 0: # 0 corresponds to the class "person" in COCO dataset 55 cv2.rectangle(frame, (box[0], box[1]), (box[0] + box[2], box[1] + box[3]), (0, 255, 0), 2) 56 cv2.putText(frame, f'{classNames[classId - 1].upper()} {round(conf * 100, 2)}', 57 (box[0] + 10, box[1] + 30), cv2.FONT_HERSHEY_COMPLEX_SMALL, 58 1, (0, 255, 0), 2) 59 60 # Write the frame into the output video 61 out.write(frame) 62 63 cv2.imshow("Video", frame) 64 if cv2.waitKey(1) & 0xFF == ord('q'): 65 break 66 67 # Release everything if job is finished 68 cap.release() 69 out.release() 70 cv2.destroyAllWindows()