Skip to content

Commit 5d706c3

Browse files
authored
Added Demo code files
1 parent 94ec40b commit 5d706c3

File tree

3 files changed

+201
-0
lines changed

3 files changed

+201
-0
lines changed

demo-raspberry.py

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import numpy as np
2+
import random as rn
3+
import tensorflow as tf
4+
from tensorflow import keras
5+
from tensorflow.keras import layers, models, optimizers
6+
from tensorflow.keras.models import Sequential
7+
from tensorflow.keras.applications import VGG16
8+
import PIL
9+
from PIL import Image
10+
import cv2
11+
import os
12+
13+
import classification
14+
import server_tcp
15+
from playsound import playsound
16+
17+
18+
if __name__ == "__main__":
19+
20+
count = 0
21+
num_image = 0
22+
flag_safe = 0
23+
flag_dist = 0
24+
flag_phone = 0
25+
26+
print("System is going to start----->\n")
27+
28+
29+
print(".......\n")
30+
dest_folder = 'image/'
31+
32+
vgg_model = tf.keras.models.load_model('model/model.h5')
33+
34+
while(True):
35+
36+
path_img = server_tcp.get_raspberry_image(dest_folder)
37+
img = cv2.imread(path_img)
38+
cv2.imshow('output',img)
39+
40+
if cv2.waitKey(20) & 0xFF == ord('q'):
41+
break
42+
43+
if len(os.listdir("image")) == 5:
44+
for image in os.listdir("image"):
45+
result = classification.classification(vgg_model,"image/"+image)
46+
print("Result: " + result)
47+
if result == "Phone":
48+
flag_phone = flag_phone + 1
49+
elif result == "Distraction":
50+
flag_dist = flag_dist + 1
51+
else:
52+
flag_safe = flag_safe + 1
53+
os.remove("image/"+image)
54+
num_image = 0
55+
56+
if max(flag_phone, flag_dist, flag_safe) == flag_dist:
57+
playsound('audio/dist.mp3')
58+
elif max(flag_phone, flag_dist, flag_safe) == flag_phone:
59+
playsound('audio/phone.mp3')
60+
elif max(flag_phone, flag_dist, flag_safe) == flag_safe:
61+
playsound('audio/safe.mp3')
62+
63+
flag_safe = 0
64+
flag_phone = 0
65+
flag_dist = 0
66+
67+

demo-webcam.py

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import numpy as np
2+
import random as rn
3+
import tensorflow as tf
4+
from tensorflow import keras
5+
from tensorflow.keras import layers, models, optimizers
6+
from tensorflow.keras.models import Sequential
7+
from tensorflow.keras.applications import VGG16
8+
import PIL
9+
from PIL import Image
10+
import cv2
11+
import os
12+
13+
import classification
14+
from playsound import playsound
15+
16+
17+
if __name__ == "__main__":
18+
19+
count = 0
20+
num_image = 0
21+
flag_safe = 0
22+
flag_dist = 0
23+
flag_phone = 0
24+
25+
print("System is going to start----->\n")
26+
27+
28+
print(".......\n")
29+
30+
mac_cam = cv2.VideoCapture(0)
31+
vgg_model = tf.keras.models.load_model('model/model.h5')
32+
33+
while(True):
34+
35+
retrieve, frames = mac_cam.read()
36+
cv2.imshow('Try2Catch', frames)
37+
count = count + 1
38+
if count == 20:
39+
40+
cv2.imwrite("image/"+str(num_image)+".png",frames)
41+
count = 0
42+
num_image = num_image + 1
43+
44+
if cv2.waitKey(20) & 0xFF == ord('q'):
45+
break
46+
47+
if len(os.listdir("image")) == 10:
48+
for image in os.listdir("image"):
49+
result = classification.classification(vgg_model,"image/"+image)
50+
print("Result: " + result)
51+
if result == "Phone":
52+
flag_phone = flag_phone + 1
53+
elif result == "Distraction":
54+
flag_dist = flag_dist + 1
55+
else:
56+
flag_safe = flag_safe + 1
57+
os.remove("image/"+image)
58+
num_image = 0
59+
60+
if max(flag_phone, flag_dist, flag_safe) == flag_dist:
61+
playsound('audio/dist.mp3')
62+
elif max(flag_phone, flag_dist, flag_safe) == flag_phone:
63+
playsound('audio/phone.mp3')
64+
elif max(flag_phone, flag_dist, flag_safe) == flag_safe:
65+
playsound('audio/safe.mp3')
66+
67+
flag_safe = 0
68+
flag_phone = 0
69+
flag_dist = 0
70+
71+

server_tcp.py

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import os
2+
import cv2
3+
import socket
4+
import numpy as np
5+
6+
7+
8+
9+
10+
HOST = "169.254.108.159" # Ethernet Interface Address PC
11+
PORT = 65432
12+
#HOST = "127.0.0.1" # The server's hostname or IP address
13+
#PORT = 80 # Port to listen on (non-privileged ports are > 1023)
14+
15+
#@dest_folder = 'image'
16+
def get_raspberry_image(dest_folder):
17+
all_data = b''
18+
i = len(os.listdir(dest_folder)) + 1
19+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
20+
s.bind((HOST, PORT))
21+
s.listen()
22+
conn, addr = s.accept()
23+
with conn:
24+
print(f"Connected by {addr}")
25+
while True:
26+
data = conn.recv(8760)
27+
#print(f"Received {data!r}")
28+
#print("dimensione dati ricevuti: ",len(data))
29+
30+
if not data: #it is true when data == b''
31+
break
32+
#all_data.extend(data) NOT WORKS
33+
all_data = b"".join([all_data, data])
34+
#print("dimensione temporanea all_data: ",len(all_data))
35+
#conn.send(data)
36+
37+
38+
#print("dimensione immagine: ",len(all_data))
39+
image = np.asarray(bytearray(all_data))
40+
image = cv2.imdecode(image, cv2.IMREAD_COLOR)
41+
path_captured_image = dest_folder + 'img%d.jpg' %i
42+
#print("percorso immagine: " + path_captured_image)
43+
cv2.imwrite(path_captured_image, image)
44+
45+
46+
return path_captured_image
47+
48+
49+
50+
51+
# convert to numpy array
52+
#image = np.asarray(bytearray(data))
53+
54+
# RGB to Grayscale
55+
#image = cv2.imdecode(image, 0)
56+
57+
# display image
58+
#cv2.imshow("output", image)
59+
60+
#decimg=cv2.imdecode(data,0)
61+
#cv2.imshow('SERVER',decimg)
62+
#cv2.waitKey(0)
63+
#cv2.destroyAllWindows()

0 commit comments

Comments
 (0)