Add hugging files
Browse files- original_app/.DS_Store +0 -0
- original_app/README.md +11 -0
- original_app/a.mp3 +0 -0
- original_app/b.mp3 +0 -0
- original_app/backend.py +88 -0
- original_app/coco.names +80 -0
- original_app/scarecrow.py +60 -0
original_app/.DS_Store
ADDED
Binary file (6.15 kB). View file
|
|
original_app/README.md
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
This is the based scarecrow application with the back-end and the scarecrow communication setup.
|
2 |
+
|
3 |
+
Inside the huggingface face application, as a demo, there is only the back-end part with some visualisation setup.
|
4 |
+
|
5 |
+
|
6 |
+
- a.mp3 -> predator sound for human
|
7 |
+
- b.mp3 -> predator sound for cell_phone
|
8 |
+
- coco.names -> labels for yolo to use
|
9 |
+
- scarecrow.py -> the application that collect video and send the stream to the back-end
|
10 |
+
- backend.py -> the application which run the model to detect animals
|
11 |
+
- yolov3.cfg & yolov3.weights -> can't be included inside huggingface as binary
|
original_app/a.mp3
ADDED
Binary file (60.2 kB). View file
|
|
original_app/b.mp3
ADDED
Binary file (56.8 kB). View file
|
|
original_app/backend.py
ADDED
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import numpy as np
|
3 |
+
import socket
|
4 |
+
import pickle
|
5 |
+
import struct
|
6 |
+
|
7 |
+
# Load YOLO model
|
8 |
+
net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg")
|
9 |
+
classes = []
|
10 |
+
with open("coco.names", "r") as f:
|
11 |
+
classes = [line.strip() for line in f.readlines()]
|
12 |
+
|
13 |
+
resolved_label = ''
|
14 |
+
|
15 |
+
# Set up socket
|
16 |
+
HOST = ''
|
17 |
+
PORT = 8089
|
18 |
+
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
19 |
+
print('Socket created')
|
20 |
+
s.bind((HOST, PORT))
|
21 |
+
print('Socket bind complete')
|
22 |
+
s.listen(10)
|
23 |
+
print('Socket now listening')
|
24 |
+
|
25 |
+
# Accept connections
|
26 |
+
conn, addr = s.accept()
|
27 |
+
|
28 |
+
# Receive and process frames
|
29 |
+
data = b''
|
30 |
+
payload_size = struct.calcsize("L")
|
31 |
+
while True:
|
32 |
+
# Retrieve message size
|
33 |
+
while len(data) < payload_size:
|
34 |
+
data += conn.recv(4096)
|
35 |
+
packed_msg_size = data[:payload_size]
|
36 |
+
data = data[payload_size:]
|
37 |
+
msg_size = struct.unpack("L", packed_msg_size)[0]
|
38 |
+
|
39 |
+
# Retrieve all data based on message size
|
40 |
+
while len(data) < msg_size:
|
41 |
+
data += conn.recv(4096)
|
42 |
+
frame_data = data[:msg_size]
|
43 |
+
data = data[msg_size:]
|
44 |
+
|
45 |
+
# Extract frame
|
46 |
+
frame = pickle.loads(frame_data)
|
47 |
+
|
48 |
+
# Run YOLO on frame
|
49 |
+
blob = cv2.dnn.blobFromImage(frame, 1/255.0, (416, 416), swapRB=True, crop=False)
|
50 |
+
net.setInput(blob)
|
51 |
+
outputs = net.forward(net.getUnconnectedOutLayersNames())
|
52 |
+
boxes = []
|
53 |
+
confidences = []
|
54 |
+
class_ids = []
|
55 |
+
for output in outputs:
|
56 |
+
for detection in output:
|
57 |
+
scores = detection[5:]
|
58 |
+
class_id = np.argmax(scores)
|
59 |
+
confidence = scores[class_id]
|
60 |
+
if confidence > 0.5:
|
61 |
+
center_x = int(detection[0] * frame.shape[1])
|
62 |
+
center_y = int(detection[1] * frame.shape[0])
|
63 |
+
w = int(detection[2] * frame.shape[1])
|
64 |
+
h = int(detection[3] * frame.shape[0])
|
65 |
+
x = int(center_x - w/2)
|
66 |
+
y = int(center_y - h/2)
|
67 |
+
boxes.append([x, y, w, h])
|
68 |
+
confidences.append(float(confidence))
|
69 |
+
class_ids.append(class_id)
|
70 |
+
indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
|
71 |
+
if len(indexes) > 0:
|
72 |
+
for i in indexes.flatten():
|
73 |
+
resolved_label = classes[class_ids[i]]
|
74 |
+
print(resolved_label)
|
75 |
+
|
76 |
+
# Display frame
|
77 |
+
cv2.imshow('frame', frame)
|
78 |
+
cv2.waitKey(1)
|
79 |
+
|
80 |
+
# Send response to client
|
81 |
+
try:
|
82 |
+
if len(indexes) > 0:
|
83 |
+
response = "[Scarecrow]: " + resolved_label
|
84 |
+
else:
|
85 |
+
response = "[Scarecrow]: NONE"
|
86 |
+
except IndexError:
|
87 |
+
response = "[Scarecrow]: ERROR"
|
88 |
+
conn.sendall(response.encode())
|
original_app/coco.names
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
person
|
2 |
+
bicycle
|
3 |
+
car
|
4 |
+
motorbike
|
5 |
+
aeroplane
|
6 |
+
bus
|
7 |
+
train
|
8 |
+
truck
|
9 |
+
boat
|
10 |
+
traffic light
|
11 |
+
fire hydrant
|
12 |
+
stop sign
|
13 |
+
parking meter
|
14 |
+
bench
|
15 |
+
bird
|
16 |
+
cat
|
17 |
+
dog
|
18 |
+
horse
|
19 |
+
sheep
|
20 |
+
cow
|
21 |
+
elephant
|
22 |
+
bear
|
23 |
+
zebra
|
24 |
+
giraffe
|
25 |
+
backpack
|
26 |
+
umbrella
|
27 |
+
handbag
|
28 |
+
tie
|
29 |
+
suitcase
|
30 |
+
frisbee
|
31 |
+
skis
|
32 |
+
snowboard
|
33 |
+
sports ball
|
34 |
+
kite
|
35 |
+
baseball bat
|
36 |
+
baseball glove
|
37 |
+
skateboard
|
38 |
+
surfboard
|
39 |
+
tennis racket
|
40 |
+
bottle
|
41 |
+
wine glass
|
42 |
+
cup
|
43 |
+
fork
|
44 |
+
knife
|
45 |
+
spoon
|
46 |
+
bowl
|
47 |
+
banana
|
48 |
+
apple
|
49 |
+
sandwich
|
50 |
+
orange
|
51 |
+
broccoli
|
52 |
+
carrot
|
53 |
+
hot dog
|
54 |
+
pizza
|
55 |
+
donut
|
56 |
+
cake
|
57 |
+
chair
|
58 |
+
sofa
|
59 |
+
pottedplant
|
60 |
+
bed
|
61 |
+
diningtable
|
62 |
+
toilet
|
63 |
+
tvmonitor
|
64 |
+
laptop
|
65 |
+
mouse
|
66 |
+
remote
|
67 |
+
keyboard
|
68 |
+
cell_phone
|
69 |
+
microwave
|
70 |
+
oven
|
71 |
+
toaster
|
72 |
+
sink
|
73 |
+
refrigerator
|
74 |
+
book
|
75 |
+
clock
|
76 |
+
vase
|
77 |
+
scissors
|
78 |
+
teddy bear
|
79 |
+
hair drier
|
80 |
+
toothbrush
|
original_app/scarecrow.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import numpy as np
|
3 |
+
import socket
|
4 |
+
import sys
|
5 |
+
import pickle
|
6 |
+
import struct
|
7 |
+
import pygame
|
8 |
+
|
9 |
+
# Initialize pygame mixer
|
10 |
+
pygame.mixer.init()
|
11 |
+
|
12 |
+
# Set up dictionary of label-sound pairs
|
13 |
+
label_sound_dict = {
|
14 |
+
"person": "a.mp3",
|
15 |
+
"cell_phone": "b.mp3",
|
16 |
+
}
|
17 |
+
|
18 |
+
cap = cv2.VideoCapture(0)
|
19 |
+
clientsocket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
|
20 |
+
clientsocket.connect(('localhost',8089))
|
21 |
+
|
22 |
+
# Initialize variables for label detection
|
23 |
+
label_count = 0
|
24 |
+
current_label = None
|
25 |
+
|
26 |
+
while True:
|
27 |
+
ret, frame = cap.read()
|
28 |
+
# Serialize frame
|
29 |
+
data = pickle.dumps(frame)
|
30 |
+
|
31 |
+
# Send message length first
|
32 |
+
message_size = struct.pack("L", len(data)) ### CHANGED
|
33 |
+
|
34 |
+
# Then data
|
35 |
+
clientsocket.sendall(message_size + data)
|
36 |
+
|
37 |
+
# Receive response from server
|
38 |
+
response = clientsocket.recv(1024)
|
39 |
+
decoded_response = response.decode()
|
40 |
+
print(decoded_response)
|
41 |
+
|
42 |
+
# Check if response is a label we care about
|
43 |
+
if "[Scarecrow]: " in decoded_response:
|
44 |
+
label = decoded_response.split("[Scarecrow]: ")[1]
|
45 |
+
if label in label_sound_dict:
|
46 |
+
if label != current_label:
|
47 |
+
# Reset label count if new label detected
|
48 |
+
current_label = label
|
49 |
+
label_count = 1
|
50 |
+
else:
|
51 |
+
# Increment label count if same label detected
|
52 |
+
label_count += 1
|
53 |
+
if label_count >= 5: # play sound if label detected 5 times in a row
|
54 |
+
sound_file = label_sound_dict[label]
|
55 |
+
sound = pygame.mixer.Sound(sound_file)
|
56 |
+
sound.play()
|
57 |
+
label_count = 0
|
58 |
+
else:
|
59 |
+
current_label = None
|
60 |
+
label_count = 0
|