File size: 1,682 Bytes
edcb323 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
import cv2
import numpy as np
import socket
import sys
import pickle
import struct
import pygame
# Initialize pygame mixer
pygame.mixer.init()
# Set up dictionary of label-sound pairs
label_sound_dict = {
"person": "a.mp3",
"cell_phone": "b.mp3",
}
cap = cv2.VideoCapture(0)
clientsocket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
clientsocket.connect(('localhost',8089))
# Initialize variables for label detection
label_count = 0
current_label = None
while True:
ret, frame = cap.read()
# Serialize frame
data = pickle.dumps(frame)
# Send message length first
message_size = struct.pack("L", len(data)) ### CHANGED
# Then data
clientsocket.sendall(message_size + data)
# Receive response from server
response = clientsocket.recv(1024)
decoded_response = response.decode()
print(decoded_response)
# Check if response is a label we care about
if "[Scarecrow]: " in decoded_response:
label = decoded_response.split("[Scarecrow]: ")[1]
if label in label_sound_dict:
if label != current_label:
# Reset label count if new label detected
current_label = label
label_count = 1
else:
# Increment label count if same label detected
label_count += 1
if label_count >= 5: # play sound if label detected 5 times in a row
sound_file = label_sound_dict[label]
sound = pygame.mixer.Sound(sound_file)
sound.play()
label_count = 0
else:
current_label = None
label_count = 0
|