-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_rasberrypi.py
165 lines (121 loc) · 5.45 KB
/
run_rasberrypi.py
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
mport cv2
import mediapipe as mp
import time
import os
from ultralytics import YOLOv10
class HandSignDetector:
def __init__(self, saved_model_path):
self.model_path= saved_model_path
self.detector = YOLOv10(saved_model_path)
def detect(self, image):
detected_results = self.detector(image)
class_names = list()
if detected_results and len(detected_results) > 0:
for result in detected_results: # ì¬ë¬ ì´ë¯¸ì§€ì 결과가 ìì ì ìì
if result.boxes and result.boxes.cls is not None:
class_indices = result.boxes.cls.cpu().numpy() # í´ëì¤ ì¸ë±ì¤ ì¶ì¶
class_names = [result.names[int(cls_idx)] for cls_idx in class_indices] # í´ëì¤ ì´ë¦ ë³€í
print("Predicted class names:", class_names)
else:
print("No objects detected.")
return class_names
# Mediapipe ???
mp_face_detection = mp.solutions.face_detection
hand_sign_detector = HandSignDetector("/home/pi/Desktop/best.pt")
mp_drawing = mp.solutions.drawing_utils
# ??? ???
#!/usr/bin/python3
import cv2
from picamera2 import Picamera2
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
# Relay 1
GPIO.setup(21, GPIO.OUT)
# Relay 2
GPIO.setup(26, GPIO.OUT)
# Grab images as numpy arrays and leave everything else to OpenCV.
cv2.startWindowThread()
picam2 = Picamera2()
picam2.configure(picam2.create_preview_configuration(main={"size": (1200, 900)}))
picam2.start()
# ?? ?? ?? ?? ??
face_detected_time = None # ??? ?? ??? ??
save_image_delay = 3 # ??? ??? ? ????? ?? ?? (?)
with mp_face_detection.FaceDetection(model_selection=0, min_detection_confidence=0.7) as face_detection:
while True:
im = picam2.capture_array()
image_rgb = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
image = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
import requests
url = "http://34.42.223.216:3505/api/a-mango/return_locking"
response = requests.get(url)
if response.json()[0] == 'lock':
GPIO.output(21, GPIO.HIGH)
print('Relay 1 ON')
if response.json()[1] == 'lock':
GPIO.output(26, GPIO.HIGH)
print('Relay 2 ON')
if response.json()[0] == 'unlock':
GPIO.output(21, GPIO.LOW)
print("Relay 1 OFF")
if response.json()[1] == 'unlock':
GPIO.output(26, GPIO.LOW)
print("Relay 2 OFF")
# ?? ?? ??
results = face_detection.process(image)
detected_hand_sign = hand_sign_detector.detect(image)
# ???? BGR? ?? (OpenCV?)
if results.detections:
if detected_hand_sign is not None:
# ??? ???
if face_detected_time is None:
# ??? ?? ???? ? ?? ??
face_detected_time = time.time()
else:
# ??? ??? 3? ???? ??
if time.time() - face_detected_time >= save_image_delay:
# ??? ??
cv2.imwrite('/home/pi/Pictures/saved_image.jpg', image_rgb)
print("image saved!")
import requests
# ???? ?? ??
file_path = "/home/pi/Pictures/saved_image.jpg"
# ??? ?? POST ???? ??
with open(file_path, 'rb') as file:
files = {'file': (file_path, file, 'image/jpeg')}
response = requests.post("http://34.42.223.216:3505/api/a-mango/face_recognition", files=files)
# ?? ?? ??
if response.status_code == 200:
print("Success:", response.json())
else:
print("Error:", response.json())
print(response.json())
if response.json()['first_storage_room_lock'] == 'unlock':
GPIO.output(21, GPIO.LOW)
print('1번 보관함 OPEN')
if response.json()['second_storage_room_lock'] == 'unlock':
GPIO.output(26, GPIO.LOW)
print('2번 보관함 OPEN')
if response.json()['first_storage_room_lock'] == 'lock':
GPIO.output(21, GPIO.HIGH)
print('1번 보관함 LOCK')
if response.json()['second_storage_room_lock'] == 'lock':
GPIO.output(26, GPIO.HIGH)
print('2번 보관함 LOCK')
face_detected_time = None # ?? ???
else:
# ??? ???? ??? ?? ???
face_detected_time = None
# ??? ?? ?? ???
if results.detections:
for detection in results.detections:
mp_drawing.draw_detection(image, detection)
# ??? ??
#cv2.imshow('MediaPipe Face Detection', cv2.flip(image, 1))
# ????? imshow ?? ?? ??
# ESC? ?? ??
if cv2.waitKey(5) & 0xFF == 27:
break
cap.release()
cv2.destroyAllWindows()