Skip to content

Commit 86b7eb4

Browse files
committed
update face recognition
1 parent 7b009d1 commit 86b7eb4

File tree

129 files changed

+15828
-39
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

129 files changed

+15828
-39
lines changed

Diff for: .idea/crowd_control_web.iml

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: app-5.py

+139
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
from flask import Flask, jsonify
2+
from flask_mqtt import Mqtt
3+
from fatigue_detector import YOLOv11FatigueDetector
4+
from crowd_detector import YOLOv11CrowdDetector
5+
from PIL import Image
6+
from io import BytesIO
7+
import cv2
8+
import logging
9+
import json
10+
from datetime import datetime
11+
import numpy as np
12+
import base64
13+
import time
14+
import threading
15+
import gc
16+
17+
from ultralytics import YOLO
18+
19+
app = Flask(__name__)
20+
21+
# initialize model
22+
try:
23+
crowd_detector = YOLOv11CrowdDetector()
24+
# fatigue_detector = YOLOv11FatigueDetector()
25+
except Exception as e:
26+
logging.error("Gagal menginisialisasi YOLOv11CrowdDetector: %s", e)
27+
28+
# MQTT Configuration
29+
app.config['MQTT_BROKER_URL'] = 'localhost'
30+
app.config['MQTT_BROKER_PORT'] = 1883
31+
# app.config['MQTT_USERNAME'] = ''
32+
# app.config['MQTT_PASSWORD'] = ''
33+
app.config['MQTT_REFRESH_TIME'] = 1.0
34+
35+
# Initialize MQTT
36+
mqtt = Mqtt(app)
37+
38+
# Subscription Topics
39+
CROWD_FRAME_TOPIC = 'mqtt-crowd-frame'
40+
FATIGUE_FRAME_TOPIC = 'mqtt-fatigue-frame'
41+
42+
# Publication Topics
43+
CROWD_RESULT_TOPIC = 'mqtt-crowd-result'
44+
FATIGUE_RESULT_TOPIC = 'mqtt-fatigue-result'
45+
46+
# Global variables to store latest received messages
47+
latest_crowd_frame = None
48+
latest_fatigue_frame = None
49+
50+
51+
# Base64 to image
52+
# Fungsi untuk Memproses Frame dari Data Base64
53+
def process_frame(frame_data):
54+
try:
55+
if ',' in frame_data:
56+
frame_data = frame_data.split(',')[1]
57+
frame_bytes = base64.b64decode(frame_data)
58+
frame_pil = Image.open(BytesIO(frame_bytes))
59+
frame = cv2.cvtColor(np.array(frame_pil), cv2.COLOR_RGB2BGR)
60+
return frame
61+
except Exception as e:
62+
logging.error(f"Error processing frame: {e}")
63+
return None
64+
65+
66+
# konversi objek numpy.ndarray menjadi list
67+
def custom_serializer(obj):
68+
if isinstance(obj, np.ndarray):
69+
return obj.tolist()
70+
elif isinstance(obj, (np.float32, np.float64)):
71+
return float(obj) # Konversi ke tipe float native Python
72+
elif isinstance(obj, (np.int32, np.int64)):
73+
return int(obj) # Konversi ke tipe int native Python
74+
raise TypeError(f"Type {type(obj)} not serializable")
75+
76+
77+
@mqtt.on_connect()
78+
def handle_connect(client, userdata, flags, rc):
79+
print('Connected to MQTT Broker')
80+
81+
# Subscribe to topics
82+
mqtt.subscribe(CROWD_FRAME_TOPIC)
83+
mqtt.subscribe(FATIGUE_FRAME_TOPIC)
84+
85+
print(f'Subscribed to {CROWD_FRAME_TOPIC} and {FATIGUE_FRAME_TOPIC}')
86+
87+
88+
@mqtt.on_message()
89+
def handle_mqtt_message(clientt, userdata, message):
90+
global latest_crowd_frame, latest_fatigue_frame
91+
topic = message.topic
92+
payload = message.payload.decode('utf-8')
93+
94+
try:
95+
# parse the payload
96+
data = json.loads(payload.replace("'", '"'))
97+
frame_data = data['frame']
98+
id = data['id']
99+
100+
if topic == CROWD_FRAME_TOPIC:
101+
latest_crowd_frame = frame_data
102+
# print(latest_crowd_frame)
103+
104+
# proccess frame
105+
frame = process_frame(latest_crowd_frame)
106+
107+
frame, detection_data = crowd_detector.detect_and_annotate(frame)
108+
num_people = len(detection_data)
109+
110+
# process crowd frame and publish result
111+
mqtt.publish(f'{CROWD_RESULT_TOPIC}-{id}', json.dumps({
112+
'detection_data': detection_data,
113+
'num_people': num_people
114+
}))
115+
116+
# elif topic == FATIGUE_FRAME_TOPIC:
117+
# latest_fatigue_frame = data
118+
# # print(latest_fatigue_frame)
119+
#
120+
# # process fatigue frame and
121+
# frame = process_frame(data)
122+
#
123+
# frame, detection_results = fatigue_detector.detect_and_annotate(frame)
124+
# fatigue_status = fatigue_detector.get_fatigue_category(detection_results)
125+
#
126+
# # publish result
127+
# mqtt.publish(FATIGUE_RESULT_TOPIC, json.dumps({
128+
# 'detection_result': detection_results,
129+
# 'fatigue_status': fatigue_status
130+
# }, default=custom_serializer))
131+
132+
except json.JSONDecodeError:
133+
print(f'Error decoding JSON from topic {topic}')
134+
except Exception as e:
135+
print(f'Error processing message from {topic}: {e}')
136+
137+
138+
if __name__ == '__app__':
139+
app.run(debug=True)

Diff for: app-7.py

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
from flask import Flask, jsonify
2+
from flask_mqtt import Mqtt
3+
from crowd_detector import YOLOv11CrowdDetector
4+
from fatigue_detector import YOLOv11FatigueDetector
5+
from PIL import Image
6+
from io import BytesIO
7+
import cv2
8+
import logging
9+
import json
10+
import numpy as np
11+
import base64
12+
13+
14+
from ultralytics import YOLO
15+
16+
app = Flask(__name__)
17+
18+
# try:
19+
# crowd_detector = YOLOv11CrowdDetector()
20+
# crowd_detector = YOLOv11FatigueDetector()
21+
# print(crowd_detector)
22+
# model = YOLO('yolov8n.pt')
23+
# except Exception as e:
24+
# logging.error("Gagal menginisialisasi YOLOv11CrowdDetector: %s", e)
25+
26+
# MQTT Configuration
27+
app.config['MQTT_BROKER_URL'] = 'localhost'
28+
app.config['MQTT_BROKER_PORT'] = 1883
29+
# app.config['MQTT_USERNAME'] = ''
30+
# app.config['MQTT_PASSWORD'] = ''
31+
app.config['MQTT_REFRESH_TIME'] = 1.0
32+
33+
# Initialize MQTT
34+
mqtt = Mqtt(app)
35+
36+
# Subscription Topics
37+
CROWD_FRAME_TOPIC = 'mqtt-crowd-frame'
38+
FATIGUE_FRAME_TOPIC = 'mqtt-fatigue-frame'
39+
40+
# Publication Topics
41+
CROWD_RESULT_TOPIC = 'mqtt-crowd-result'
42+
FATIGUE_RESULT_TOPIC = 'mqtt-fatigue-result'
43+
44+
# Global variables to store latest received messages
45+
latest_crowd_frame = None
46+
latest_fatigue_frame = None
47+
48+
# Base64 to image
49+
# Fungsi untuk Memproses Frame dari Data Base64
50+
def process_frame(frame_data):
51+
try:
52+
if ',' in frame_data:
53+
frame_data = frame_data.split(',')[1]
54+
frame_bytes = base64.b64decode(frame_data)
55+
frame_pil = Image.open(BytesIO(frame_bytes))
56+
frame = cv2.cvtColor(np.array(frame_pil), cv2.COLOR_RGB2BGR)
57+
return frame
58+
except Exception as e:
59+
logging.error(f"Error processing frame: {e}")
60+
return None
61+
62+
@mqtt.on_connect()
63+
def handle_connect(client, userdata, flags, rc):
64+
print('Connected to MQTT Broker')
65+
66+
# Subscribe to topics
67+
mqtt.subscribe(CROWD_FRAME_TOPIC)
68+
mqtt.subscribe(FATIGUE_FRAME_TOPIC)
69+
70+
print(f'Subscribed to {CROWD_FRAME_TOPIC} and {FATIGUE_FRAME_TOPIC}')
71+
72+
73+
@mqtt.on_message()
74+
def handle_mqtt_message(clientt, userdata, message):
75+
global latest_crowd_frame, latest_fatigue_frame
76+
topic = message.topic
77+
payload = message.payload.decode('utf-8')
78+
79+
try:
80+
# parse the payload
81+
data = json.loads(payload)
82+
83+
if topic == CROWD_FRAME_TOPIC:
84+
latest_crowd_frame = data
85+
# process crowd frame and publish result
86+
mqtt.publish(CROWD_RESULT_TOPIC, json.dumps({'message': 'test-crowd'}))
87+
88+
elif topic == FATIGUE_FRAME_TOPIC:
89+
latest_fatigue_frame = data
90+
# process fatigue frame and publish result
91+
mqtt.publish(CROWD_RESULT_TOPIC, json.dumps({'detection_data': 'test-fatigue'}))
92+
93+
94+
except json.JSONDecodeError:
95+
print(f'Error decoding JSON from topic {topic}')
96+
except Exception as e:
97+
print(f'Error processing message from {topic}: {e}')
98+
99+
100+
101+
if __name__ == '__app__':
102+
app.run(debug=True)

Diff for: app.py

+5-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from flask import Flask, jsonify
1+
from flask import Flask
22
from flask_mqtt import Mqtt
33
from fatigue_detector import YOLOv11FatigueDetector
44
from crowd_detector import YOLOv11CrowdDetector
@@ -7,12 +7,8 @@
77
import cv2
88
import logging
99
import json
10-
from datetime import datetime
1110
import numpy as np
1211
import base64
13-
import time
14-
import threading
15-
import gc
1612

1713
from ultralytics import YOLO
1814

@@ -119,12 +115,12 @@ def handle_mqtt_message(clientt, userdata, message):
119115
frame = process_frame(data)
120116

121117
frame, detection_results = fatigue_detector.detect_and_annotate(frame)
122-
fatigue_status = fatigue_detector.get_fatigue_category(detection_results)
118+
fatigue_status = fatigue_detector.get_fatigue_category(frame)
123119

124120
# publish result
125121
mqtt.publish(FATIGUE_RESULT_TOPIC, json.dumps({
126-
'detection_result': detection_results,
127-
'fatigue_status': fatigue_status
122+
'detection_data': detection_results,
123+
'status': fatigue_status
128124
}, default=custom_serializer))
129125

130126
except json.JSONDecodeError:
@@ -134,4 +130,4 @@ def handle_mqtt_message(clientt, userdata, message):
134130

135131

136132
if __name__ == '__app__':
137-
app.run(debug=True)
133+
app.run(debug=True, port=5000)

0 commit comments

Comments
 (0)