Spaces:
Runtime error
Runtime error
Upload 8 files
Browse files- app.py +394 -0
- app_finish.py +384 -0
- best.onnx +3 -0
- camera.py +154 -0
- database.py +106 -0
- recognition (1).py +341 -0
- report.py +296 -0
- requirements.txt +16 -0
app.py
ADDED
|
@@ -0,0 +1,394 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from fpdf import FPDF
|
| 3 |
+
from datetime import datetime
|
| 4 |
+
import zipfile
|
| 5 |
+
import os
|
| 6 |
+
import requests
|
| 7 |
+
import subprocess
|
| 8 |
+
from PIL import Image
|
| 9 |
+
from recognition import FaceDetection
|
| 10 |
+
import re
|
| 11 |
+
from report import ReportGenerator
|
| 12 |
+
from dotenv import set_key, load_dotenv
|
| 13 |
+
import cv2
|
| 14 |
+
import time
|
| 15 |
+
base_dir = './temp' ###เชื่อมตรงนี้จ้าๆๆๆๆๆๆๆ
|
| 16 |
+
from camera import ObjectTracker
|
| 17 |
+
|
| 18 |
+
# def run_external_command(command):
|
| 19 |
+
# try:
|
| 20 |
+
# # Execute the command
|
| 21 |
+
# result = subprocess.run(command, capture_output=True, text=True, shell=True)
|
| 22 |
+
# return result.stdout if result.returncode == 0 else result.stderr
|
| 23 |
+
# except Exception as e:
|
| 24 |
+
# return f"Error: {str(e)}"
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def is_invalid_date(date_str):
|
| 28 |
+
# Define a regex pattern for YYYY-MM-DD format
|
| 29 |
+
pattern = r'^\d{4}-(0[1-9]|1[0-2])$'
|
| 30 |
+
return re.match(pattern, date_str) is None
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
# Function to generate a PDF with selected date range or single date
|
| 34 |
+
def generate_pdf(start_date, end_date):
|
| 35 |
+
try:
|
| 36 |
+
start_valid_date = datetime.strptime(start_date, "%Y-%m")
|
| 37 |
+
if end_date.strip() == "":
|
| 38 |
+
end_valid_date = start_valid_date
|
| 39 |
+
else:
|
| 40 |
+
end_valid_date = datetime.strptime(end_date, "%Y-%m")
|
| 41 |
+
|
| 42 |
+
if start_valid_date >= end_valid_date:
|
| 43 |
+
return None, "Error: Start date must be before end date."
|
| 44 |
+
|
| 45 |
+
if is_invalid_date(start_date) or is_invalid_date(end_date):
|
| 46 |
+
return None, "Invalid date format. Please use YYYY-MM."
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
formatted_start_date = start_valid_date.strftime("%b %Y")
|
| 50 |
+
formatted_end_date = end_valid_date.strftime("%b %Y")
|
| 51 |
+
|
| 52 |
+
report = ReportGenerator(formatted_start_date, formatted_end_date)
|
| 53 |
+
file_name = report.generate()
|
| 54 |
+
|
| 55 |
+
return file_name, None # Return filename and no error
|
| 56 |
+
except ValueError:
|
| 57 |
+
return None, "Invalid date format. Please use YYYY-MM."
|
| 58 |
+
|
| 59 |
+
#register
|
| 60 |
+
def convert_named_string_to_bytes(named_string):
|
| 61 |
+
"""Convert NamedString object to bytes."""
|
| 62 |
+
if hasattr(named_string, 'data'):
|
| 63 |
+
return named_string.data # Return the data directly if it has a data attribute
|
| 64 |
+
return bytes(named_string, 'utf-8') # Convert to bytes if it's not a NamedString
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def convert_named_string_to_bytes(named_string):
|
| 68 |
+
# สมมติว่ามีวิธีการในการแปลง NamedString เป็น bytes
|
| 69 |
+
if hasattr(named_string, 'read'):
|
| 70 |
+
return named_string.read() # หากเป็นไฟล์ที่สามารถอ่านได้
|
| 71 |
+
else:
|
| 72 |
+
return bytes(named_string, 'utf-8') # หากเป็น string ธรรมดา
|
| 73 |
+
|
| 74 |
+
def register_student(name, student_id, image_file=None, zip_file=None):
|
| 75 |
+
try:
|
| 76 |
+
# Convert name to string and student_id to integer
|
| 77 |
+
name = str(name)
|
| 78 |
+
student_id = int(student_id)
|
| 79 |
+
|
| 80 |
+
# Validate that both name and student ID are provided
|
| 81 |
+
if not name or not student_id:
|
| 82 |
+
return "Error: Please provide both student name and student ID."
|
| 83 |
+
|
| 84 |
+
# Clean the name for safe directory naming
|
| 85 |
+
safe_name = name
|
| 86 |
+
safe_student_id = str(student_id) # Keep student_id as a string for directory naming
|
| 87 |
+
|
| 88 |
+
# Create a directory to store student images
|
| 89 |
+
student_dir = os.path.join(base_dir, f"{safe_student_id}_{safe_name}")
|
| 90 |
+
os.makedirs(student_dir, exist_ok=True)
|
| 91 |
+
|
| 92 |
+
# Handle single image upload (JPEG)
|
| 93 |
+
if image_file is not None:
|
| 94 |
+
img = Image.open(image_file)
|
| 95 |
+
# Create the path for the uploaded image
|
| 96 |
+
image_path = os.path.join(student_dir, f"{safe_student_id}_{safe_name}.jpg")
|
| 97 |
+
img.save(image_path)
|
| 98 |
+
|
| 99 |
+
process = FaceDetection(capture=img)
|
| 100 |
+
process.manual_regis(student_dir)
|
| 101 |
+
|
| 102 |
+
# Handle ZIP file upload
|
| 103 |
+
if zip_file is not None:
|
| 104 |
+
zip_path = os.path.join(student_dir, f"{safe_student_id}_{safe_name}_images.zip")
|
| 105 |
+
with open(zip_path, "wb") as f:
|
| 106 |
+
f.write(convert_named_string_to_bytes(zip_file)) # Convert to bytes before writing
|
| 107 |
+
|
| 108 |
+
# Extract ZIP file to the student's directory
|
| 109 |
+
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
|
| 110 |
+
zip_ref.extractall(student_dir)
|
| 111 |
+
|
| 112 |
+
return f"Student {name} (ID: {student_id}) registered successfully!"
|
| 113 |
+
|
| 114 |
+
except zipfile.BadZipFile:
|
| 115 |
+
return "Error: The uploaded file is not a valid ZIP file."
|
| 116 |
+
except Exception as e:
|
| 117 |
+
return f"Error during registration: {str(e)}"
|
| 118 |
+
|
| 119 |
+
# การจัดการผลลัพธ์จาก generate_pdf
|
| 120 |
+
def handle_pdf(start_date, end_date):
|
| 121 |
+
file_name, error = generate_pdf(start_date, end_date)
|
| 122 |
+
if error:
|
| 123 |
+
return None, error # Return None and the error message
|
| 124 |
+
return file_name, None # Return the filename and None for error
|
| 125 |
+
|
| 126 |
+
# Help function to explain the available commands with examples
|
| 127 |
+
def help_function():
|
| 128 |
+
"""
|
| 129 |
+
**Available Commands:**
|
| 130 |
+
1. **upload zip**: Upload a ZIP file containing images for face recognition.
|
| 131 |
+
- Example: upload zip my_images.zip
|
| 132 |
+
2. **request zip**: Enter a date range to generate a ZIP file of images of people not wearing helmets.
|
| 133 |
+
- Example: request zip 2023-01 2023-01
|
| 134 |
+
3. **upload video**: Upload a video for object detection (provide the date of the video).
|
| 135 |
+
- Example: upload video my_video.mp4 2023-01
|
| 136 |
+
4. **camera**: Enter the stream URL to configure the camera.
|
| 137 |
+
- Example: camera http://100.86.155.229:8080/video
|
| 138 |
+
5. **pdf**: Generate a PDF for the selected date range.
|
| 139 |
+
- Example: pdf 2023-01 2023-01
|
| 140 |
+
"""
|
| 141 |
+
help_text = """
|
| 142 |
+
**Available Commands:**
|
| 143 |
+
1. **upload video**: Upload a video for object detection (provide the date of the video).
|
| 144 |
+
- Example: upload video my_video.mp4 2023-01
|
| 145 |
+
2. **camera**: Enter the stream URL to configure the camera.
|
| 146 |
+
- Example: camera http://100.86.155.229:8080/video
|
| 147 |
+
"""
|
| 148 |
+
return help_text
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
# Function to upload zip file for face recognition
|
| 152 |
+
def upload_zip(file):
|
| 153 |
+
try:
|
| 154 |
+
with zipfile.ZipFile(file, 'r') as zip_ref:
|
| 155 |
+
zip_ref.extractall('C:/zoom/AI/gradio/my_images.zip') # Change the path to your desired directory
|
| 156 |
+
return "ZIP file uploaded and extracted successfully!"
|
| 157 |
+
except Exception as e:
|
| 158 |
+
return str(e)
|
| 159 |
+
|
| 160 |
+
# Function to generate zip based on date range
|
| 161 |
+
def generate_zip_by_date(start_date, end_date):
|
| 162 |
+
try:
|
| 163 |
+
start_valid_date = datetime.strptime(start_date, "%Y-%m-%d")
|
| 164 |
+
end_valid_date = datetime.strptime(end_date, "%Y-%m-%d")
|
| 165 |
+
|
| 166 |
+
zip_file_name = f"no_helmet_images_{start_date}_to_{end_date}.zip"
|
| 167 |
+
with zipfile.ZipFile(zip_file_name, 'w') as zipf:
|
| 168 |
+
for root, dirs, files in os.walk('/path/to/images'): # Change this path to your image directory
|
| 169 |
+
for file in files:
|
| 170 |
+
file_date = datetime.strptime(file.split('_')[0], '%Y-%m-%d') # Assuming the file has the date
|
| 171 |
+
if start_valid_date <= file_date <= end_valid_date:
|
| 172 |
+
zipf.write(os.path.join(root, file))
|
| 173 |
+
return zip_file_name
|
| 174 |
+
except Exception as e:
|
| 175 |
+
return str(e)
|
| 176 |
+
|
| 177 |
+
# Function to upload video and detect objects
|
| 178 |
+
def upload_video(file, date):
|
| 179 |
+
return f"Video from {date} processed successfully!"
|
| 180 |
+
|
| 181 |
+
# Function to set IP for the camera
|
| 182 |
+
def set_camera_ip(ip_address):
|
| 183 |
+
env_file = ".env"
|
| 184 |
+
set_key(env_file, "CAMERA_URL", ip_address)
|
| 185 |
+
load_dotenv(override=True)
|
| 186 |
+
print(ip_address)
|
| 187 |
+
return f"Camera IP set to {os.getenv('CAMERA_URL')}"
|
| 188 |
+
|
| 189 |
+
|
| 190 |
+
def chatbot_response(user_input, chat_history):
|
| 191 |
+
if user_input.lower().startswith("help"):
|
| 192 |
+
# แยกคำสั่งและพารามิเตอร์
|
| 193 |
+
parts = user_input.split()
|
| 194 |
+
response = help_function()
|
| 195 |
+
elif user_input.lower().startswith("camera"):
|
| 196 |
+
# แยกคำสั่งและพารามิเตอร์
|
| 197 |
+
parts = user_input.split()
|
| 198 |
+
if len(parts) == 2:
|
| 199 |
+
ip_address = parts[1]
|
| 200 |
+
response = set_camera_ip(ip_address)
|
| 201 |
+
else:
|
| 202 |
+
response = "Error: Please provide camera url."
|
| 203 |
+
|
| 204 |
+
|
| 205 |
+
else:
|
| 206 |
+
response = "Unknown command. Type 'help' to see the available commands."
|
| 207 |
+
|
| 208 |
+
# อัปเดตประวัติการสนทนา
|
| 209 |
+
chat_history.append((user_input, response))
|
| 210 |
+
|
| 211 |
+
return chat_history, chat_history
|
| 212 |
+
|
| 213 |
+
streaming = False
|
| 214 |
+
|
| 215 |
+
def get_frame():
|
| 216 |
+
"""Generator function to yield frames from the IP camera."""
|
| 217 |
+
global streaming
|
| 218 |
+
cap = cv2.VideoCapture(str(os.getenv('CAMERA_URL'))) # Open the video stream
|
| 219 |
+
|
| 220 |
+
while streaming:
|
| 221 |
+
ret, frame = cap.read() # Read a frame
|
| 222 |
+
if not ret:
|
| 223 |
+
break # Stop if no more frames
|
| 224 |
+
|
| 225 |
+
# Resize the frame (optional)
|
| 226 |
+
frame = cv2.resize(frame, (640, 480))
|
| 227 |
+
|
| 228 |
+
# Convert the frame from BGR to RGB
|
| 229 |
+
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 230 |
+
|
| 231 |
+
yield frame # Yield the frame for Gradio to display
|
| 232 |
+
|
| 233 |
+
time.sleep(0.03)
|
| 234 |
+
|
| 235 |
+
cap.release() # Release the video stream
|
| 236 |
+
|
| 237 |
+
|
| 238 |
+
tracker = ObjectTracker()
|
| 239 |
+
|
| 240 |
+
# Function to explore a directory and list its contents recursively with indentation
|
| 241 |
+
def explore_directory(path, depth=0):
|
| 242 |
+
try:
|
| 243 |
+
# Get the list of files and directories
|
| 244 |
+
contents = os.listdir(path)
|
| 245 |
+
items = []
|
| 246 |
+
|
| 247 |
+
for item in contents:
|
| 248 |
+
item_path = os.path.join(path, item)
|
| 249 |
+
indent = " " * (depth * 4) # Indentation for each depth level using non-breaking spaces
|
| 250 |
+
|
| 251 |
+
# Append directories with expandable details
|
| 252 |
+
if os.path.isdir(item_path):
|
| 253 |
+
nested_contents = explore_directory(item_path, depth + 1) # Recursively explore subdirectories
|
| 254 |
+
items.append(f"<details><summary>{indent}📁 {item}</summary>{nested_contents}</details>")
|
| 255 |
+
else:
|
| 256 |
+
if "./faces/__faces__/" in item_path:
|
| 257 |
+
item_path = item_path[18:]
|
| 258 |
+
elif "./faces/__full_pics__/" in item_path:
|
| 259 |
+
item_path = item_path[22:]
|
| 260 |
+
items.append(f'{indent}📄 {item} <span style="opacity: 0.5; margin-left:30px">path: {item_path}</span>') # Add file with icon
|
| 261 |
+
|
| 262 |
+
return "<br>".join(items)
|
| 263 |
+
except Exception as e:
|
| 264 |
+
return f"Error: {str(e)}" # Return error message if any
|
| 265 |
+
|
| 266 |
+
# Main function to handle path input and explore
|
| 267 |
+
def on_explore(path="./faces"):
|
| 268 |
+
return explore_directory(path)
|
| 269 |
+
|
| 270 |
+
def show_image(path):
|
| 271 |
+
# Read the image from the file path
|
| 272 |
+
img_Full = cv2.imread(f"./faces/__faces__/{path}")
|
| 273 |
+
img_head = cv2.imread(f"./faces/__full_pics__/{path}")
|
| 274 |
+
# Convert the image from BGR (OpenCV default) to RGB
|
| 275 |
+
img_Full = cv2.cvtColor(img_Full, cv2.COLOR_BGR2RGB)
|
| 276 |
+
img_head = cv2.cvtColor(img_head, cv2.COLOR_BGR2RGB)
|
| 277 |
+
return img_Full,img_head
|
| 278 |
+
|
| 279 |
+
|
| 280 |
+
# Gradio Interface
|
| 281 |
+
with gr.Blocks() as demo:
|
| 282 |
+
with gr.Tab("Home"):
|
| 283 |
+
gr.Markdown("### Welcome to the Command Center")
|
| 284 |
+
gr.Markdown("**Type 'help' to see the list of available commands.**")
|
| 285 |
+
|
| 286 |
+
#chatbot_widget = gr.Chatbot(label="Chat with the bot")
|
| 287 |
+
chatbot = gr.Chatbot()
|
| 288 |
+
user_input = gr.Textbox(label="Type your command here", placeholder="Enter your command...")
|
| 289 |
+
|
| 290 |
+
|
| 291 |
+
with gr.Tab("Generate PDF"):
|
| 292 |
+
date_input_start = gr.Textbox(label="Start Date (YYYY-MM)", placeholder="YYYY-MM")
|
| 293 |
+
date_input_end = gr.Textbox(label="End Date (Leave blank for same as start date)", placeholder="YYYY-MM")
|
| 294 |
+
pdf_output = gr.File(label="Generated PDF")
|
| 295 |
+
pdf_btn = gr.Button("Generate PDF")
|
| 296 |
+
|
| 297 |
+
# Generate PDF when the button is clicked
|
| 298 |
+
pdf_btn.click(fn=handle_pdf,
|
| 299 |
+
inputs=[date_input_start, date_input_end],
|
| 300 |
+
outputs=[pdf_output, gr.Textbox(label="Error Message", visible=True)])
|
| 301 |
+
|
| 302 |
+
with gr.Tab("Live Stream"):
|
| 303 |
+
#ip_camera_url = "http://100.86.155.229:8080/video" # เปลี่ยน URL ตามที่อยู่ IP ของกล้อง
|
| 304 |
+
#live_image_output = gr.Image(label="Live Stream", interactive=False)
|
| 305 |
+
start_button = gr.Button("Start/Stop Stream")
|
| 306 |
+
|
| 307 |
+
def toggle():
|
| 308 |
+
global streaming
|
| 309 |
+
streaming = not streaming
|
| 310 |
+
|
| 311 |
+
start_button.click(toggle)
|
| 312 |
+
stream = gr.Interface(
|
| 313 |
+
fn=get_frame, # Function to fetch the latest frame
|
| 314 |
+
inputs=[],
|
| 315 |
+
outputs=gr.Image(type="numpy", label="Live IP Camera Stream"),
|
| 316 |
+
live=True # Enable continuous frame updates
|
| 317 |
+
)
|
| 318 |
+
|
| 319 |
+
markdown_content = """
|
| 320 |
+
<div style="text-align: center;">
|
| 321 |
+
<h2>Or upload video</h2>
|
| 322 |
+
</div>
|
| 323 |
+
"""
|
| 324 |
+
gr.Markdown(markdown_content)
|
| 325 |
+
|
| 326 |
+
start_button = gr.Button("Stop Process")
|
| 327 |
+
|
| 328 |
+
def toggle():
|
| 329 |
+
global tracker
|
| 330 |
+
try:
|
| 331 |
+
tracker.start = False
|
| 332 |
+
except: pass
|
| 333 |
+
|
| 334 |
+
start_button.click(toggle)
|
| 335 |
+
|
| 336 |
+
with gr.Row():
|
| 337 |
+
# video_input = gr.File(label="Upload your video", file_types=[".mp4", ".avi", ".mov"])
|
| 338 |
+
|
| 339 |
+
#video_output = gr.Video(label="Processed Video Output")
|
| 340 |
+
video_output = gr.Interface(
|
| 341 |
+
fn=tracker.detect_and_track_2, # Function to fetch the latest frame
|
| 342 |
+
inputs="video",
|
| 343 |
+
outputs=gr.Image(type="numpy", label="Process"),
|
| 344 |
+
live=False, # Enable continuous frame updates
|
| 345 |
+
flagging_mode = "never"
|
| 346 |
+
)
|
| 347 |
+
|
| 348 |
+
#video_input.change(process_video, inputs=video_input, outputs=video_output)
|
| 349 |
+
|
| 350 |
+
# กด Enter เพื่อส่งข้อความ
|
| 351 |
+
user_input.submit(chatbot_response,
|
| 352 |
+
inputs=[user_input, chatbot],
|
| 353 |
+
outputs=[chatbot, chatbot])
|
| 354 |
+
|
| 355 |
+
|
| 356 |
+
# Adding Register tab
|
| 357 |
+
with gr.Tab("Register"):
|
| 358 |
+
gr.Markdown("### Register student")
|
| 359 |
+
name_input = gr.Textbox(label="Student Name", placeholder="Enter student name")
|
| 360 |
+
student_id_input = gr.Textbox(label="Student ID", placeholder="Enter student ID")
|
| 361 |
+
image_input = gr.File(label="Upload a image or a Zip file", file_types=["image", ".jpg", ".zip"], file_count="single")
|
| 362 |
+
register_button = gr.Button("Register Student")
|
| 363 |
+
register_output = gr.Textbox(label="Registration Output", placeholder="Result will be shown here")
|
| 364 |
+
|
| 365 |
+
# Register student when button is clicked
|
| 366 |
+
register_button.click(fn=register_student,
|
| 367 |
+
inputs=[name_input, student_id_input, image_input],
|
| 368 |
+
outputs=[register_output])
|
| 369 |
+
|
| 370 |
+
|
| 371 |
+
# Gradio interface
|
| 372 |
+
with gr.Tab("File explorer"):
|
| 373 |
+
gr.Markdown("# File Explorer")
|
| 374 |
+
# path_input = gr.Textbox(label="Enter Directory Path", placeholder="e.g., C:/Users/YourUsername/Documents")
|
| 375 |
+
output_display = gr.HTML(label="Contents")
|
| 376 |
+
explore_button = gr.Button("Explore")
|
| 377 |
+
|
| 378 |
+
# Set up button clicks
|
| 379 |
+
explore_button.click(fn=on_explore, outputs=output_display)
|
| 380 |
+
# explore_button.click(fn=on_explore, inputs=path_input, outputs=output_display)
|
| 381 |
+
|
| 382 |
+
interface = gr.Interface(
|
| 383 |
+
fn=show_image, # Function to display the image
|
| 384 |
+
inputs=gr.Textbox(label="Enter Directory Path", placeholder="e.g., name_of_folder/name_of_image"),
|
| 385 |
+
outputs= [gr.Image(label="Head"),
|
| 386 |
+
gr.Image(label="Full image")], # Output type is an image
|
| 387 |
+
flagging_mode = "never"
|
| 388 |
+
)
|
| 389 |
+
|
| 390 |
+
|
| 391 |
+
load_dotenv(override=True)
|
| 392 |
+
|
| 393 |
+
# Launch the Gradio app
|
| 394 |
+
demo.launch(share=True)
|
app_finish.py
ADDED
|
@@ -0,0 +1,384 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from fpdf import FPDF
|
| 3 |
+
from datetime import datetime
|
| 4 |
+
import zipfile
|
| 5 |
+
import os
|
| 6 |
+
import requests
|
| 7 |
+
import subprocess
|
| 8 |
+
from PIL import Image
|
| 9 |
+
from recognition import FaceDetection
|
| 10 |
+
import re
|
| 11 |
+
from report import ReportGenerator
|
| 12 |
+
from dotenv import set_key, load_dotenv
|
| 13 |
+
import cv2
|
| 14 |
+
import time
|
| 15 |
+
base_dir = './temp' ###เชื่อมตรงนี้จ้าๆๆๆๆๆๆๆ
|
| 16 |
+
from camera import ObjectTracker
|
| 17 |
+
|
| 18 |
+
# def run_external_command(command):
|
| 19 |
+
# try:
|
| 20 |
+
# # Execute the command
|
| 21 |
+
# result = subprocess.run(command, capture_output=True, text=True, shell=True)
|
| 22 |
+
# return result.stdout if result.returncode == 0 else result.stderr
|
| 23 |
+
# except Exception as e:
|
| 24 |
+
# return f"Error: {str(e)}"
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def is_invalid_date(date_str):
|
| 28 |
+
# Define a regex pattern for YYYY-MM-DD format
|
| 29 |
+
pattern = r'^\d{4}-(0[1-9]|1[0-2])$'
|
| 30 |
+
return re.match(pattern, date_str) is None
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
# Function to generate a PDF with selected date range or single date
|
| 34 |
+
def generate_pdf(start_date, end_date):
|
| 35 |
+
try:
|
| 36 |
+
start_valid_date = datetime.strptime(start_date, "%Y-%m")
|
| 37 |
+
if end_date.strip() == "":
|
| 38 |
+
end_valid_date = start_valid_date
|
| 39 |
+
else:
|
| 40 |
+
end_valid_date = datetime.strptime(end_date, "%Y-%m")
|
| 41 |
+
|
| 42 |
+
if start_valid_date >= end_valid_date:
|
| 43 |
+
return None, "Error: Start date must be before end date."
|
| 44 |
+
|
| 45 |
+
if is_invalid_date(start_date) or is_invalid_date(end_date):
|
| 46 |
+
return None, "Invalid date format. Please use YYYY-MM."
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
formatted_start_date = start_valid_date.strftime("%b %Y")
|
| 50 |
+
formatted_end_date = end_valid_date.strftime("%b %Y")
|
| 51 |
+
|
| 52 |
+
report = ReportGenerator(formatted_start_date, formatted_end_date)
|
| 53 |
+
file_name = report.generate()
|
| 54 |
+
|
| 55 |
+
return file_name, None # Return filename and no error
|
| 56 |
+
except ValueError:
|
| 57 |
+
return None, "Invalid date format. Please use YYYY-MM."
|
| 58 |
+
|
| 59 |
+
#register
|
| 60 |
+
def convert_named_string_to_bytes(named_string):
|
| 61 |
+
"""Convert NamedString object to bytes."""
|
| 62 |
+
if hasattr(named_string, 'data'):
|
| 63 |
+
return named_string.data # Return the data directly if it has a data attribute
|
| 64 |
+
return bytes(named_string, 'utf-8') # Convert to bytes if it's not a NamedString
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def convert_named_string_to_bytes(named_string):
|
| 68 |
+
# สมมติว่ามีวิธีการในการแปลง NamedString เป็น bytes
|
| 69 |
+
if hasattr(named_string, 'read'):
|
| 70 |
+
return named_string.read() # หากเป็นไฟล์ที่สามารถอ่านได้
|
| 71 |
+
else:
|
| 72 |
+
return bytes(named_string, 'utf-8') # หากเป็น string ธรรมดา
|
| 73 |
+
|
| 74 |
+
def register_student(name, student_id, image_file=None, zip_file=None):
|
| 75 |
+
try:
|
| 76 |
+
# Convert name to string and student_id to integer
|
| 77 |
+
name = str(name)
|
| 78 |
+
student_id = int(student_id)
|
| 79 |
+
|
| 80 |
+
# Validate that both name and student ID are provided
|
| 81 |
+
if not name or not student_id:
|
| 82 |
+
return "Error: Please provide both student name and student ID."
|
| 83 |
+
|
| 84 |
+
# Clean the name for safe directory naming
|
| 85 |
+
safe_name = name
|
| 86 |
+
safe_student_id = str(student_id) # Keep student_id as a string for directory naming
|
| 87 |
+
|
| 88 |
+
# Create a directory to store student images
|
| 89 |
+
student_dir = os.path.join(base_dir, f"{safe_student_id}_{safe_name}")
|
| 90 |
+
os.makedirs(student_dir, exist_ok=True)
|
| 91 |
+
|
| 92 |
+
# Handle single image upload (JPEG)
|
| 93 |
+
if image_file is not None:
|
| 94 |
+
img = Image.open(image_file)
|
| 95 |
+
# Create the path for the uploaded image
|
| 96 |
+
image_path = os.path.join(student_dir, f"{safe_student_id}_{safe_name}.jpg")
|
| 97 |
+
img.save(image_path)
|
| 98 |
+
|
| 99 |
+
process = FaceDetection(capture=img)
|
| 100 |
+
process.manual_regis(student_dir)
|
| 101 |
+
|
| 102 |
+
# Handle ZIP file upload
|
| 103 |
+
if zip_file is not None:
|
| 104 |
+
zip_path = os.path.join(student_dir, f"{safe_student_id}_{safe_name}_images.zip")
|
| 105 |
+
with open(zip_path, "wb") as f:
|
| 106 |
+
f.write(convert_named_string_to_bytes(zip_file)) # Convert to bytes before writing
|
| 107 |
+
|
| 108 |
+
# Extract ZIP file to the student's directory
|
| 109 |
+
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
|
| 110 |
+
zip_ref.extractall(student_dir)
|
| 111 |
+
|
| 112 |
+
return f"Student {name} (ID: {student_id}) registered successfully!"
|
| 113 |
+
|
| 114 |
+
except zipfile.BadZipFile:
|
| 115 |
+
return "Error: The uploaded file is not a valid ZIP file."
|
| 116 |
+
except Exception as e:
|
| 117 |
+
return f"Error during registration: {str(e)}"
|
| 118 |
+
|
| 119 |
+
# การจัดการผลลัพธ์จาก generate_pdf
|
| 120 |
+
def handle_pdf(start_date, end_date):
|
| 121 |
+
file_name, error = generate_pdf(start_date, end_date)
|
| 122 |
+
if error:
|
| 123 |
+
return None, error # Return None and the error message
|
| 124 |
+
return file_name, None # Return the filename and None for error
|
| 125 |
+
|
| 126 |
+
# Help function to explain the available commands with examples
|
| 127 |
+
def help_function():
|
| 128 |
+
"""
|
| 129 |
+
**Available Commands:**
|
| 130 |
+
1. **upload zip**: Upload a ZIP file containing images for face recognition.
|
| 131 |
+
- Example: upload zip my_images.zip
|
| 132 |
+
2. **request zip**: Enter a date range to generate a ZIP file of images of people not wearing helmets.
|
| 133 |
+
- Example: request zip 2023-01 2023-01
|
| 134 |
+
3. **upload video**: Upload a video for object detection (provide the date of the video).
|
| 135 |
+
- Example: upload video my_video.mp4 2023-01
|
| 136 |
+
4. **camera**: Enter the stream URL to configure the camera.
|
| 137 |
+
- Example: camera http://100.86.155.229:8080/video
|
| 138 |
+
5. **pdf**: Generate a PDF for the selected date range.
|
| 139 |
+
- Example: pdf 2023-01 2023-01
|
| 140 |
+
"""
|
| 141 |
+
help_text = """
|
| 142 |
+
**Available Commands:**
|
| 143 |
+
1. **upload video**: Upload a video for object detection (provide the date of the video).
|
| 144 |
+
- Example: upload video my_video.mp4 2023-01
|
| 145 |
+
2. **camera**: Enter the stream URL to configure the camera.
|
| 146 |
+
- Example: camera http://100.86.155.229:8080/video
|
| 147 |
+
"""
|
| 148 |
+
return help_text
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
# Function to upload zip file for face recognition
|
| 152 |
+
def upload_zip(file):
|
| 153 |
+
try:
|
| 154 |
+
with zipfile.ZipFile(file, 'r') as zip_ref:
|
| 155 |
+
zip_ref.extractall('C:/zoom/AI/gradio/my_images.zip') # Change the path to your desired directory
|
| 156 |
+
return "ZIP file uploaded and extracted successfully!"
|
| 157 |
+
except Exception as e:
|
| 158 |
+
return str(e)
|
| 159 |
+
|
| 160 |
+
# Function to generate zip based on date range
|
| 161 |
+
def generate_zip_by_date(start_date, end_date):
|
| 162 |
+
try:
|
| 163 |
+
start_valid_date = datetime.strptime(start_date, "%Y-%m-%d")
|
| 164 |
+
end_valid_date = datetime.strptime(end_date, "%Y-%m-%d")
|
| 165 |
+
|
| 166 |
+
zip_file_name = f"no_helmet_images_{start_date}_to_{end_date}.zip"
|
| 167 |
+
with zipfile.ZipFile(zip_file_name, 'w') as zipf:
|
| 168 |
+
for root, dirs, files in os.walk('/path/to/images'): # Change this path to your image directory
|
| 169 |
+
for file in files:
|
| 170 |
+
file_date = datetime.strptime(file.split('_')[0], '%Y-%m-%d') # Assuming the file has the date
|
| 171 |
+
if start_valid_date <= file_date <= end_valid_date:
|
| 172 |
+
zipf.write(os.path.join(root, file))
|
| 173 |
+
return zip_file_name
|
| 174 |
+
except Exception as e:
|
| 175 |
+
return str(e)
|
| 176 |
+
|
| 177 |
+
# Function to upload video and detect objects
|
| 178 |
+
def upload_video(file, date):
|
| 179 |
+
return f"Video from {date} processed successfully!"
|
| 180 |
+
|
| 181 |
+
# Function to set IP for the camera
|
| 182 |
+
def set_camera_ip(ip_address):
|
| 183 |
+
env_file = ".env"
|
| 184 |
+
set_key(env_file, "CAMERA_URL", ip_address)
|
| 185 |
+
load_dotenv(override=True)
|
| 186 |
+
print(ip_address)
|
| 187 |
+
return f"Camera IP set to {os.getenv('CAMERA_URL')}"
|
| 188 |
+
|
| 189 |
+
|
| 190 |
+
def chatbot_response(user_input, chat_history):
|
| 191 |
+
if user_input.lower().startswith("help"):
|
| 192 |
+
# แยกคำสั่งและพารามิเตอร์
|
| 193 |
+
parts = user_input.split()
|
| 194 |
+
response = help_function()
|
| 195 |
+
elif user_input.lower().startswith("camera"):
|
| 196 |
+
# แยกคำสั่งและพารามิเตอร์
|
| 197 |
+
parts = user_input.split()
|
| 198 |
+
if len(parts) == 2:
|
| 199 |
+
ip_address = parts[1]
|
| 200 |
+
response = set_camera_ip(ip_address)
|
| 201 |
+
else:
|
| 202 |
+
response = "Error: Please provide camera url."
|
| 203 |
+
|
| 204 |
+
|
| 205 |
+
else:
|
| 206 |
+
response = "Unknown command. Type 'help' to see the available commands."
|
| 207 |
+
|
| 208 |
+
# อัปเดตประวัติการสนทนา
|
| 209 |
+
chat_history.append((user_input, response))
|
| 210 |
+
|
| 211 |
+
return chat_history, chat_history
|
| 212 |
+
|
| 213 |
+
streaming = False
|
| 214 |
+
|
| 215 |
+
def get_frame():
|
| 216 |
+
"""Generator function to yield frames from the IP camera."""
|
| 217 |
+
global streaming
|
| 218 |
+
cap = cv2.VideoCapture(str(os.getenv('CAMERA_URL'))) # Open the video stream
|
| 219 |
+
|
| 220 |
+
while streaming:
|
| 221 |
+
ret, frame = cap.read() # Read a frame
|
| 222 |
+
if not ret:
|
| 223 |
+
break # Stop if no more frames
|
| 224 |
+
|
| 225 |
+
# Resize the frame (optional)
|
| 226 |
+
frame = cv2.resize(frame, (640, 480))
|
| 227 |
+
|
| 228 |
+
# Convert the frame from BGR to RGB
|
| 229 |
+
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 230 |
+
|
| 231 |
+
yield frame # Yield the frame for Gradio to display
|
| 232 |
+
|
| 233 |
+
time.sleep(0.03)
|
| 234 |
+
|
| 235 |
+
cap.release() # Release the video stream
|
| 236 |
+
|
| 237 |
+
|
| 238 |
+
tracker = ObjectTracker()
|
| 239 |
+
|
| 240 |
+
# Function to explore a directory and list its contents recursively with indentation
|
| 241 |
+
def explore_directory(path, depth=0):
|
| 242 |
+
try:
|
| 243 |
+
# Get the list of files and directories
|
| 244 |
+
contents = os.listdir(path)
|
| 245 |
+
items = []
|
| 246 |
+
|
| 247 |
+
for item in contents:
|
| 248 |
+
item_path = os.path.join(path, item)
|
| 249 |
+
indent = " " * (depth * 4) # Indentation for each depth level using non-breaking spaces
|
| 250 |
+
|
| 251 |
+
# Append directories with expandable details
|
| 252 |
+
if os.path.isdir(item_path):
|
| 253 |
+
nested_contents = explore_directory(item_path, depth + 1) # Recursively explore subdirectories
|
| 254 |
+
items.append(f"<details><summary>{indent}📁 {item}</summary>{nested_contents}</details>")
|
| 255 |
+
else:
|
| 256 |
+
items.append(f"{indent}📄 {item}") # Add file with icon
|
| 257 |
+
|
| 258 |
+
return "<br>".join(items)
|
| 259 |
+
except Exception as e:
|
| 260 |
+
return f"Error: {str(e)}" # Return error message if any
|
| 261 |
+
|
| 262 |
+
# Main function to handle path input and explore
|
| 263 |
+
def on_explore(path):
|
| 264 |
+
return explore_directory(path)
|
| 265 |
+
|
| 266 |
+
def show_image(path):
|
| 267 |
+
# Read the image from the file path
|
| 268 |
+
img = cv2.imread(path)
|
| 269 |
+
# Convert the image from BGR (OpenCV default) to RGB
|
| 270 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 271 |
+
return img
|
| 272 |
+
|
| 273 |
+
|
| 274 |
+
# Gradio Interface
|
| 275 |
+
with gr.Blocks() as demo:
|
| 276 |
+
with gr.Tab("Home"):
|
| 277 |
+
gr.Markdown("### Welcome to the Command Center")
|
| 278 |
+
gr.Markdown("**Type 'help' to see the list of available commands.**")
|
| 279 |
+
|
| 280 |
+
#chatbot_widget = gr.Chatbot(label="Chat with the bot")
|
| 281 |
+
chatbot = gr.Chatbot()
|
| 282 |
+
user_input = gr.Textbox(label="Type your command here", placeholder="Enter your command...")
|
| 283 |
+
|
| 284 |
+
|
| 285 |
+
with gr.Tab("Generate PDF"):
|
| 286 |
+
date_input_start = gr.Textbox(label="Start Date (YYYY-MM)", placeholder="YYYY-MM")
|
| 287 |
+
date_input_end = gr.Textbox(label="End Date (Leave blank for same as start date)", placeholder="YYYY-MM")
|
| 288 |
+
pdf_output = gr.File(label="Generated PDF")
|
| 289 |
+
pdf_btn = gr.Button("Generate PDF")
|
| 290 |
+
|
| 291 |
+
# Generate PDF when the button is clicked
|
| 292 |
+
pdf_btn.click(fn=handle_pdf,
|
| 293 |
+
inputs=[date_input_start, date_input_end],
|
| 294 |
+
outputs=[pdf_output, gr.Textbox(label="Error Message", visible=True)])
|
| 295 |
+
|
| 296 |
+
with gr.Tab("Live Stream"):
|
| 297 |
+
#ip_camera_url = "http://100.86.155.229:8080/video" # เปลี่ยน URL ตามที่อยู่ IP ของกล้อง
|
| 298 |
+
#live_image_output = gr.Image(label="Live Stream", interactive=False)
|
| 299 |
+
start_button = gr.Button("Start/Stop Stream")
|
| 300 |
+
|
| 301 |
+
def toggle():
|
| 302 |
+
global streaming
|
| 303 |
+
streaming = not streaming
|
| 304 |
+
|
| 305 |
+
start_button.click(toggle)
|
| 306 |
+
stream = gr.Interface(
|
| 307 |
+
fn=get_frame, # Function to fetch the latest frame
|
| 308 |
+
inputs=[],
|
| 309 |
+
outputs=gr.Image(type="numpy", label="Live IP Camera Stream"),
|
| 310 |
+
live=True # Enable continuous frame updates
|
| 311 |
+
)
|
| 312 |
+
|
| 313 |
+
markdown_content = """
|
| 314 |
+
<div style="text-align: center;">
|
| 315 |
+
<h2>Or upload video</h2>
|
| 316 |
+
</div>
|
| 317 |
+
"""
|
| 318 |
+
gr.Markdown(markdown_content)
|
| 319 |
+
|
| 320 |
+
start_button = gr.Button("Stop Process")
|
| 321 |
+
|
| 322 |
+
def toggle():
|
| 323 |
+
global tracker
|
| 324 |
+
try:
|
| 325 |
+
tracker.start = False
|
| 326 |
+
except: pass
|
| 327 |
+
|
| 328 |
+
start_button.click(toggle)
|
| 329 |
+
|
| 330 |
+
with gr.Row():
|
| 331 |
+
# video_input = gr.File(label="Upload your video", file_types=[".mp4", ".avi", ".mov"])
|
| 332 |
+
|
| 333 |
+
#video_output = gr.Video(label="Processed Video Output")
|
| 334 |
+
video_output = gr.Interface(
|
| 335 |
+
fn=tracker.detect_and_track_2, # Function to fetch the latest frame
|
| 336 |
+
inputs="video",
|
| 337 |
+
outputs=gr.Image(type="numpy", label="Process"),
|
| 338 |
+
live=False # Enable continuous frame updates
|
| 339 |
+
)
|
| 340 |
+
|
| 341 |
+
#video_input.change(process_video, inputs=video_input, outputs=video_output)
|
| 342 |
+
|
| 343 |
+
# กด Enter เพื่อส่งข้อความ
|
| 344 |
+
user_input.submit(chatbot_response,
|
| 345 |
+
inputs=[user_input, chatbot],
|
| 346 |
+
outputs=[chatbot, chatbot])
|
| 347 |
+
|
| 348 |
+
|
| 349 |
+
# Adding Register tab
|
| 350 |
+
with gr.Tab("Register"):
|
| 351 |
+
gr.Markdown("### Register student")
|
| 352 |
+
name_input = gr.Textbox(label="Student Name", placeholder="Enter student name")
|
| 353 |
+
student_id_input = gr.Textbox(label="Student ID", placeholder="Enter student ID")
|
| 354 |
+
image_input = gr.File(label="Upload a image or a Zip file", file_types=["image", ".jpg", ".zip"], file_count="single")
|
| 355 |
+
register_button = gr.Button("Register Student")
|
| 356 |
+
register_output = gr.Textbox(label="Registration Output", placeholder="Result will be shown here")
|
| 357 |
+
|
| 358 |
+
# Register student when button is clicked
|
| 359 |
+
register_button.click(fn=register_student,
|
| 360 |
+
inputs=[name_input, student_id_input, image_input],
|
| 361 |
+
outputs=[register_output])
|
| 362 |
+
|
| 363 |
+
|
| 364 |
+
# Gradio interface
|
| 365 |
+
with gr.Tab("File explorer"):
|
| 366 |
+
gr.Markdown("# File Explorer")
|
| 367 |
+
path_input = gr.Textbox(label="Enter Directory Path", placeholder="e.g., C:/Users/YourUsername/Documents")
|
| 368 |
+
output_display = gr.HTML(label="Contents")
|
| 369 |
+
explore_button = gr.Button("Explore")
|
| 370 |
+
|
| 371 |
+
# Set up button clicks
|
| 372 |
+
explore_button.click(fn=on_explore, inputs=path_input, outputs=output_display)
|
| 373 |
+
|
| 374 |
+
interface = gr.Interface(
|
| 375 |
+
fn=show_image, # Function to display the image
|
| 376 |
+
inputs=gr.Textbox(label="Enter Directory Path", placeholder="e.g., C:/Users/YourUsername/Documents"),
|
| 377 |
+
outputs="image" # Output type is an image
|
| 378 |
+
)
|
| 379 |
+
|
| 380 |
+
|
| 381 |
+
load_dotenv(override=True)
|
| 382 |
+
|
| 383 |
+
# Launch the Gradio app
|
| 384 |
+
demo.launch(share=True)
|
best.onnx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:06f3e8951d48d87fcad7ad8302505c5be0b210c1413eaa03ed90c6e320d6a53e
|
| 3 |
+
size 12252561
|
camera.py
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from ultralytics import YOLO
|
| 2 |
+
import subprocess
|
| 3 |
+
import threading
|
| 4 |
+
import os
|
| 5 |
+
import time
|
| 6 |
+
import cv2
|
| 7 |
+
from PIL import Image
|
| 8 |
+
import threading
|
| 9 |
+
|
| 10 |
+
class ObjectTracker:
|
| 11 |
+
def __init__(self, video_source=None, model_path='best.onnx'):
|
| 12 |
+
# Initialize the YOLO model and video capture
|
| 13 |
+
self.model = YOLO(model_path, task="detect")
|
| 14 |
+
if video_source is not None:
|
| 15 |
+
self.cap = cv2.VideoCapture(video_source)
|
| 16 |
+
|
| 17 |
+
# Object classes and tracking information
|
| 18 |
+
self.class_names = ["head", "helmet", "motorbike"]
|
| 19 |
+
self.passing_ids = set()
|
| 20 |
+
self.previous_centers = {}
|
| 21 |
+
self.threads = []
|
| 22 |
+
|
| 23 |
+
self.start = True
|
| 24 |
+
|
| 25 |
+
#
|
| 26 |
+
def process_object(self, buffer):
|
| 27 |
+
"""Run subprocess to process object asynchronously."""
|
| 28 |
+
process = subprocess.Popen(['python', 'recognition.py'], stdin=subprocess.PIPE)
|
| 29 |
+
process.communicate(input=buffer.tobytes())
|
| 30 |
+
print(2)
|
| 31 |
+
|
| 32 |
+
def detect_and_track(self):
|
| 33 |
+
"""Main loop for detecting and tracking objects."""
|
| 34 |
+
while True:
|
| 35 |
+
ret, frame = self.cap.read()
|
| 36 |
+
if not ret:
|
| 37 |
+
break # Stop if no frame is returned
|
| 38 |
+
|
| 39 |
+
frame = cv2.resize(frame, (600, 600))
|
| 40 |
+
motorbike_detect = list(self.model.track(frame, persist=True, conf=0.65, classes=[2]))
|
| 41 |
+
annotated_frame = motorbike_detect[0].plot()
|
| 42 |
+
|
| 43 |
+
self.handle_detections(motorbike_detect, frame)
|
| 44 |
+
cv2.imshow('Webcam', annotated_frame)
|
| 45 |
+
|
| 46 |
+
if cv2.waitKey(1) == ord('q'):
|
| 47 |
+
break
|
| 48 |
+
|
| 49 |
+
self.cap.release()
|
| 50 |
+
cv2.destroyAllWindows()
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def detect_and_track_2(self, video):
|
| 54 |
+
self.cap = cv2.VideoCapture(video)
|
| 55 |
+
self.start = True
|
| 56 |
+
"""Main loop for detecting and tracking objects."""
|
| 57 |
+
while self.start:
|
| 58 |
+
ret, frame = self.cap.read()
|
| 59 |
+
if not ret:
|
| 60 |
+
break # Stop if no frame is returned
|
| 61 |
+
|
| 62 |
+
frame = cv2.resize(frame, (600, 600))
|
| 63 |
+
motorbike_detect = list(self.model.track(frame, persist=True, conf=0.65, classes=[2]))
|
| 64 |
+
annotated_frame = motorbike_detect[0].plot()
|
| 65 |
+
|
| 66 |
+
self.handle_detections(motorbike_detect, frame)
|
| 67 |
+
yield annotated_frame
|
| 68 |
+
|
| 69 |
+
if cv2.waitKey(1) == ord('q'):
|
| 70 |
+
break
|
| 71 |
+
|
| 72 |
+
self.cap.release()
|
| 73 |
+
cv2.destroyAllWindows()
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def handle_detections(self, detections, frame):
|
| 77 |
+
"""Process detected objects and track their movement."""
|
| 78 |
+
for result in detections[0].boxes.data.tolist():
|
| 79 |
+
if len(result) == 7: # Ensure detection has all necessary attributes
|
| 80 |
+
x1, y1, x2, y2, obj_id, conf, label = result
|
| 81 |
+
|
| 82 |
+
if obj_id not in self.previous_centers:
|
| 83 |
+
self.previous_centers[obj_id] = 0
|
| 84 |
+
|
| 85 |
+
center_x = int((x1 + x2) / 2)
|
| 86 |
+
print(f"id: {obj_id} {self.previous_centers[obj_id]} {center_x}")
|
| 87 |
+
|
| 88 |
+
if self.crossed_line(obj_id, center_x):
|
| 89 |
+
self.save_and_process_object(frame, x1, y1, x2, y2, obj_id)
|
| 90 |
+
|
| 91 |
+
self.previous_centers[obj_id] = center_x
|
| 92 |
+
|
| 93 |
+
def crossed_line(self, obj_id, center_x):
|
| 94 |
+
"""Check if the object has crossed the predefined line."""
|
| 95 |
+
return (
|
| 96 |
+
self.previous_centers[obj_id] >= 150 and
|
| 97 |
+
center_x <= 150 and
|
| 98 |
+
obj_id not in self.passing_ids
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
def save_and_process_object(self, frame, x1, y1, x2, y2, obj_id):
|
| 102 |
+
"""Save the object image and process it in a new thread."""
|
| 103 |
+
self.passing_ids.add(obj_id)
|
| 104 |
+
print(f"id:{obj_id} crossed the line")
|
| 105 |
+
|
| 106 |
+
img_crop = frame[int(y1 - 150):int(y2), int(x1):int(x2 + 20), :]
|
| 107 |
+
# cv2.imwrite(f'img/object_crossed{obj_id}.jpg', img_crop)
|
| 108 |
+
_, buffer = cv2.imencode('.jpg',img_crop)
|
| 109 |
+
with open(f'img/object_crossed{obj_id}.jpg', "wb") as f:
|
| 110 |
+
f.write(buffer)
|
| 111 |
+
|
| 112 |
+
print(1)
|
| 113 |
+
thread = threading.Thread(target=self.process_object, args=(buffer,))
|
| 114 |
+
thread.start()
|
| 115 |
+
self.threads.append(thread)
|
| 116 |
+
print(3)
|
| 117 |
+
|
| 118 |
+
def test_camera():
|
| 119 |
+
cap = cv2.VideoCapture('http://100.86.155.229:8080/video')
|
| 120 |
+
|
| 121 |
+
if not cap.isOpened():
|
| 122 |
+
print("Error: Could not open the webcam.")
|
| 123 |
+
else:
|
| 124 |
+
print("Press 's' to save a frame, or 'q' to quit.")
|
| 125 |
+
|
| 126 |
+
# Loop to continuously capture frames
|
| 127 |
+
while True:
|
| 128 |
+
ret, frame = cap.read() # Capture a frame
|
| 129 |
+
if not ret:
|
| 130 |
+
print("Failed to grab frame.")
|
| 131 |
+
break
|
| 132 |
+
|
| 133 |
+
# Display the frame in a window
|
| 134 |
+
cv2.imshow("Webcam Stream", frame)
|
| 135 |
+
|
| 136 |
+
# Check for key press events
|
| 137 |
+
key = cv2.waitKey(1) & 0xFF # Mask for cross-platform compatibility
|
| 138 |
+
|
| 139 |
+
if key == ord('s'): # Press 's' to save a frame
|
| 140 |
+
cv2.imwrite("saved_frame.jpg", frame) # Save the frame as a JPEG
|
| 141 |
+
print("Frame saved as 'saved_frame.jpg'.")
|
| 142 |
+
|
| 143 |
+
elif key == ord('q'): # Press 'q' to quit
|
| 144 |
+
print("Exiting...")
|
| 145 |
+
break
|
| 146 |
+
|
| 147 |
+
# Release the webcam and close the window
|
| 148 |
+
cap.release()
|
| 149 |
+
cv2.destroyAllWindows()
|
| 150 |
+
|
| 151 |
+
if __name__ == "__main__":
|
| 152 |
+
tracker = ObjectTracker()
|
| 153 |
+
tracker.detect_and_track()
|
| 154 |
+
#test_camera()
|
database.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# sudo mysql -u root -p
|
| 2 |
+
# CREATE DATABASE monitor_log;
|
| 3 |
+
# USE monitor_log;
|
| 4 |
+
# SHOW TABLES;
|
| 5 |
+
# DROP TABLE anonymous_no;
|
| 6 |
+
# DROP TABLE log;
|
| 7 |
+
# SELECT * FROM information_schema.columns WHERE table_name = 'log';
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
import mysql.connector
|
| 11 |
+
from mysql.connector import Error
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
class Database:
|
| 15 |
+
def __init__(self, host, user, password, database):
|
| 16 |
+
"""Initialize the database connection."""
|
| 17 |
+
self.host = host
|
| 18 |
+
self.user = user
|
| 19 |
+
self.password = password
|
| 20 |
+
self.database = database
|
| 21 |
+
self.connection = None
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def connect(self):
|
| 25 |
+
"""Establish a connection to the database."""
|
| 26 |
+
try:
|
| 27 |
+
self.connection = mysql.connector.connect(
|
| 28 |
+
host=self.host,
|
| 29 |
+
user=self.user,
|
| 30 |
+
password=self.password,
|
| 31 |
+
database=self.database
|
| 32 |
+
)
|
| 33 |
+
if self.connection.is_connected():
|
| 34 |
+
print("Successfully connected to the database.")
|
| 35 |
+
except Error as e:
|
| 36 |
+
print(f"Error: {e}")
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def disconnect(self):
|
| 40 |
+
"""Close the database connection."""
|
| 41 |
+
if self.connection.is_connected():
|
| 42 |
+
self.connection.close()
|
| 43 |
+
print("Database connection closed.")
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def execute_query(self, query, values=None):
|
| 47 |
+
"""Execute a query that does not return results (INSERT, UPDATE, DELETE)."""
|
| 48 |
+
cursor = self.connection.cursor()
|
| 49 |
+
try:
|
| 50 |
+
if values:
|
| 51 |
+
cursor.execute(query, values)
|
| 52 |
+
else:
|
| 53 |
+
cursor.execute(query)
|
| 54 |
+
self.connection.commit()
|
| 55 |
+
print("Query executed successfully.")
|
| 56 |
+
except Error as e:
|
| 57 |
+
print(f"Error: {e}")
|
| 58 |
+
finally:
|
| 59 |
+
cursor.close()
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def fetch_all(self, query):
|
| 63 |
+
"""Execute a SELECT query and return all results."""
|
| 64 |
+
cursor = self.connection.cursor()
|
| 65 |
+
results = []
|
| 66 |
+
try:
|
| 67 |
+
cursor.execute(query)
|
| 68 |
+
results = cursor.fetchall()
|
| 69 |
+
return results
|
| 70 |
+
except Error as e:
|
| 71 |
+
print(f"Error: {e}")
|
| 72 |
+
finally:
|
| 73 |
+
cursor.close()
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def create_table(self, create_table_query):
|
| 77 |
+
"""Create a new table in the database."""
|
| 78 |
+
cursor = self.connection.cursor()
|
| 79 |
+
try:
|
| 80 |
+
cursor.execute(create_table_query)
|
| 81 |
+
print("Table created successfully.")
|
| 82 |
+
except Error as e:
|
| 83 |
+
print(f"Error: {e}")
|
| 84 |
+
finally:
|
| 85 |
+
cursor.close()
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
if __name__ == "__main__":
|
| 90 |
+
db = Database(host='localhost', user='root', password='0123456789', database='monitor_log')
|
| 91 |
+
db.connect()
|
| 92 |
+
db.create_table("""
|
| 93 |
+
CREATE TABLE anonymous_no (
|
| 94 |
+
number INT AUTO_INCREMENT PRIMARY KEY,
|
| 95 |
+
name VARCHAR(60) NOT NULL,
|
| 96 |
+
description VARCHAR(500)
|
| 97 |
+
);
|
| 98 |
+
""")
|
| 99 |
+
db.create_table("""
|
| 100 |
+
CREATE TABLE log (
|
| 101 |
+
id VARCHAR(255) AUTO_INCREMENT PRIMARY KEY,
|
| 102 |
+
name VARCHAR(60) NOT NULL,
|
| 103 |
+
datetime DATETIME DEFAULT CURRENT_TIMESTAMP
|
| 104 |
+
);
|
| 105 |
+
""")
|
| 106 |
+
db.disconnect()
|
recognition (1).py
ADDED
|
@@ -0,0 +1,341 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from collections import defaultdict
|
| 2 |
+
from deepface import DeepFace
|
| 3 |
+
import mysql.connector
|
| 4 |
+
import numpy as np
|
| 5 |
+
import chromadb
|
| 6 |
+
import random
|
| 7 |
+
import time
|
| 8 |
+
import uuid
|
| 9 |
+
import cv2
|
| 10 |
+
import sys
|
| 11 |
+
import os
|
| 12 |
+
from ultralytics import YOLO
|
| 13 |
+
from watchdog.observers import Observer
|
| 14 |
+
from watchdog.events import FileSystemEventHandler
|
| 15 |
+
from PIL import Image
|
| 16 |
+
|
| 17 |
+
# Initialize MySQL connection
|
| 18 |
+
connection = mysql.connector.connect(
|
| 19 |
+
host="mydb.c3a48iwi2mip.ap-southeast-2.rds.amazonaws.com", user="admin", password="0123456789", database="monitor_log"
|
| 20 |
+
)
|
| 21 |
+
command = connection.cursor()
|
| 22 |
+
|
| 23 |
+
class ImageHandler(FileSystemEventHandler):
|
| 24 |
+
def on_created(self, event):
|
| 25 |
+
# When a file is created
|
| 26 |
+
if not event.is_directory: # Skip directories
|
| 27 |
+
file_path = event.src_path
|
| 28 |
+
# Check if it's an image (you can modify extensions as needed)
|
| 29 |
+
if file_path.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp', '.tiff')):
|
| 30 |
+
print(f"New image added: {file_path}")
|
| 31 |
+
# Open and process the image
|
| 32 |
+
try:
|
| 33 |
+
global image
|
| 34 |
+
image = cv2.imread(file_path)
|
| 35 |
+
print(image)
|
| 36 |
+
|
| 37 |
+
head_helmet_detect = list(model(image,conf=0.67,classes=[0,1]))
|
| 38 |
+
for result in head_helmet_detect[0].boxes.data.tolist():
|
| 39 |
+
hx1,hy1,hx2,hy2,hconf,hlabel = result
|
| 40 |
+
if hlabel == 0:
|
| 41 |
+
print(f"\n\n\n\n{hx1} {hx2} {hy1} {hy2}\n\n\n\n")
|
| 42 |
+
cimage = image[int(hy1):int(hy2), int(hx1):int(hx2), :]
|
| 43 |
+
process = FaceDetection(capture=cimage)
|
| 44 |
+
process.verify() # Or perform any other operation
|
| 45 |
+
|
| 46 |
+
except Exception as e:
|
| 47 |
+
print(f"Error opening image: {e}")
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
# Face Detection using Deepface
|
| 51 |
+
class FaceDetection:
|
| 52 |
+
# initial set-up for new create object
|
| 53 |
+
def __init__(self, capture, db="facedb", root="faces"):
|
| 54 |
+
self.cap = capture
|
| 55 |
+
|
| 56 |
+
if not os.path.exists(root):
|
| 57 |
+
os.makedirs(root)
|
| 58 |
+
|
| 59 |
+
self.root = root
|
| 60 |
+
|
| 61 |
+
self.client = chromadb.PersistentClient(root)
|
| 62 |
+
self.db = self.client.get_or_create_collection(
|
| 63 |
+
name=db,
|
| 64 |
+
metadata={
|
| 65 |
+
"hnsw:space": 'cosine',
|
| 66 |
+
},
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
self.cropped_frame = None
|
| 70 |
+
self.identity = None
|
| 71 |
+
|
| 72 |
+
# detect and crop face from image
|
| 73 |
+
def detect(self, frame, threshold=0.5):
|
| 74 |
+
cropped_frame = None
|
| 75 |
+
boundary = None
|
| 76 |
+
try:
|
| 77 |
+
face = DeepFace.extract_faces(img_path=frame)
|
| 78 |
+
|
| 79 |
+
if face[0]['confidence'] >= threshold:
|
| 80 |
+
face_data = face[0]['facial_area']
|
| 81 |
+
x, y, w, h, leye, reye = face_data.values()
|
| 82 |
+
|
| 83 |
+
cropped_frame = frame[y:y+h, x:x+w]
|
| 84 |
+
|
| 85 |
+
COLOR = (0, 255, 0)
|
| 86 |
+
THICKNESS = 2
|
| 87 |
+
START_POINT = (x, y)
|
| 88 |
+
END_POINT = (x + w, y + h)
|
| 89 |
+
|
| 90 |
+
boundary = (frame, START_POINT, END_POINT, COLOR, THICKNESS)
|
| 91 |
+
except: pass
|
| 92 |
+
|
| 93 |
+
return cropped_frame, boundary
|
| 94 |
+
|
| 95 |
+
# manually register faces to database using folder
|
| 96 |
+
def manual_regis(self,folder_path):
|
| 97 |
+
for root, dirs, files in os.walk(folder_path):
|
| 98 |
+
for file in files[:1]:
|
| 99 |
+
img_path = os.path.join(root, file)
|
| 100 |
+
img = cv2.imread(img_path)
|
| 101 |
+
|
| 102 |
+
self.cropped_frame, boundary = self.detect(img, 0.8)
|
| 103 |
+
|
| 104 |
+
if self.cropped_frame is not None and self.cropped_frame.size != 0:
|
| 105 |
+
try:
|
| 106 |
+
embedding = DeepFace.represent(img_path=self.cropped_frame, enforce_detection=False)[0]
|
| 107 |
+
print(len(embedding['embedding']))
|
| 108 |
+
|
| 109 |
+
results = self.db.query(
|
| 110 |
+
query_embeddings=[embedding['embedding']], # Replace with your unknown embeddings
|
| 111 |
+
n_results=1
|
| 112 |
+
)
|
| 113 |
+
print(results)
|
| 114 |
+
except: print(0)
|
| 115 |
+
else:
|
| 116 |
+
full_name = os.path.normpath(root).split(os.sep)[-1]
|
| 117 |
+
print(full_name)
|
| 118 |
+
|
| 119 |
+
if results['distances'][0] and results['distances'][0][0] < 0.3:
|
| 120 |
+
exist_folder = results['metadatas'][0][0].get('name')
|
| 121 |
+
|
| 122 |
+
current_path = os.path.join(self.root, '__faces__', exist_folder)
|
| 123 |
+
new_path = os.path.join(self.root, '__faces__', full_name)
|
| 124 |
+
os.rename(current_path, new_path)
|
| 125 |
+
|
| 126 |
+
fcurrent_path = os.path.join(self.root, '__full_pics__', exist_folder)
|
| 127 |
+
fnew_path = os.path.join(self.root, '__full_pics__', full_name)
|
| 128 |
+
os.rename(fcurrent_path, fnew_path)
|
| 129 |
+
|
| 130 |
+
# Update database
|
| 131 |
+
if current_path != new_path:
|
| 132 |
+
for dirpath, dirnames, existfiles in os.walk(new_path):
|
| 133 |
+
for existfile in existfiles:
|
| 134 |
+
# Get filename
|
| 135 |
+
imgname, _ = os.path.splitext(existfile)
|
| 136 |
+
print(imgname)
|
| 137 |
+
|
| 138 |
+
record = self.db.get(imgname)
|
| 139 |
+
|
| 140 |
+
if record:
|
| 141 |
+
# Update chromadb
|
| 142 |
+
current_metadata = record['metadatas']
|
| 143 |
+
current_metadata[0]['name'] = full_name
|
| 144 |
+
self.db.update(imgname, metadatas=current_metadata)
|
| 145 |
+
|
| 146 |
+
# Update mySQL
|
| 147 |
+
sql = "UPDATE log SET name = %s WHERE id = %s"
|
| 148 |
+
val = (full_name, imgname)
|
| 149 |
+
command.execute(sql, val)
|
| 150 |
+
connection.commit()
|
| 151 |
+
|
| 152 |
+
# Save new data to database
|
| 153 |
+
idx = str(uuid.uuid4())
|
| 154 |
+
filename = idx + '.jpg'
|
| 155 |
+
|
| 156 |
+
path = os.path.join(self.root, '__faces__', full_name)
|
| 157 |
+
if not os.path.exists(path):
|
| 158 |
+
os.makedirs(path)
|
| 159 |
+
cv2.imwrite(os.path.join(path, filename), self.cropped_frame)
|
| 160 |
+
|
| 161 |
+
fpath = os.path.join(self.root, '__full_pics__', full_name)
|
| 162 |
+
if not os.path.exists(fpath):
|
| 163 |
+
os.makedirs(fpath)
|
| 164 |
+
cv2.imwrite(os.path.join(fpath, filename), img)
|
| 165 |
+
|
| 166 |
+
self.db.add(
|
| 167 |
+
ids=[idx],
|
| 168 |
+
embeddings=[embedding['embedding']],
|
| 169 |
+
metadatas=[{'name': full_name}]
|
| 170 |
+
)
|
| 171 |
+
|
| 172 |
+
# Insert to mySQL
|
| 173 |
+
sql = "INSERT INTO log (id, name) VALUES (%s, %s)"
|
| 174 |
+
val = (idx, full_name)
|
| 175 |
+
command.execute(sql, val)
|
| 176 |
+
connection.commit()
|
| 177 |
+
|
| 178 |
+
# Save other leftover faces in the folder
|
| 179 |
+
for file in files[1:]:
|
| 180 |
+
img_path = os.path.join(root, file)
|
| 181 |
+
img = cv2.imread(img_path)
|
| 182 |
+
|
| 183 |
+
self.cropped_frame, boundary = self.detect(img, 0.8)
|
| 184 |
+
|
| 185 |
+
if self.cropped_frame is not None and self.cropped_frame.size != 0:
|
| 186 |
+
try:
|
| 187 |
+
embedding = DeepFace.represent(img_path=self.cropped_frame, enforce_detection=False)[0]
|
| 188 |
+
print(len(embedding['embedding']))
|
| 189 |
+
except: pass
|
| 190 |
+
else:
|
| 191 |
+
# Save new data to database
|
| 192 |
+
idx = str(uuid.uuid4())
|
| 193 |
+
filename = idx + '.jpg'
|
| 194 |
+
|
| 195 |
+
path = os.path.join(self.root, '__faces__', full_name)
|
| 196 |
+
cv2.imwrite(os.path.join(path, filename), self.cropped_frame)
|
| 197 |
+
|
| 198 |
+
fpath = os.path.join(self.root, '__full_pics__', full_name)
|
| 199 |
+
cv2.imwrite(os.path.join(fpath, filename), img)
|
| 200 |
+
|
| 201 |
+
self.db.add(
|
| 202 |
+
ids=[idx],
|
| 203 |
+
embeddings=[embedding['embedding']],
|
| 204 |
+
metadatas=[{'name': full_name}]
|
| 205 |
+
)
|
| 206 |
+
|
| 207 |
+
# Insert to mySQL
|
| 208 |
+
sql = "INSERT INTO log (id, name, scheck) VALUES (%s, %s)"
|
| 209 |
+
val = (idx, full_name)
|
| 210 |
+
command.execute(sql, val)
|
| 211 |
+
connection.commit()
|
| 212 |
+
|
| 213 |
+
print("Successfully registered faces in folder")
|
| 214 |
+
|
| 215 |
+
# register face to database (run automaticlly in the program sequence)
|
| 216 |
+
def auto_regis(self,name,key='0'):
|
| 217 |
+
if self.cropped_frame is not None and self.cropped_frame.size != 0:
|
| 218 |
+
try:
|
| 219 |
+
embedding = DeepFace.represent(img_path=self.cropped_frame, enforce_detection=False)[0]
|
| 220 |
+
print(len(embedding['embedding']))
|
| 221 |
+
except: print(0)
|
| 222 |
+
else:
|
| 223 |
+
|
| 224 |
+
full_name = name
|
| 225 |
+
print(full_name)
|
| 226 |
+
|
| 227 |
+
if key == '0':
|
| 228 |
+
idx = str(uuid.uuid4())
|
| 229 |
+
else: idx = key
|
| 230 |
+
|
| 231 |
+
filename = idx + '.jpg'
|
| 232 |
+
|
| 233 |
+
path = os.path.join(self.root, '__faces__', full_name)
|
| 234 |
+
if not os.path.exists(path):
|
| 235 |
+
os.makedirs(path)
|
| 236 |
+
cv2.imwrite(os.path.join(path, filename), self.cropped_frame)
|
| 237 |
+
|
| 238 |
+
fpath = os.path.join(self.root, '__full_pics__', full_name)
|
| 239 |
+
if not os.path.exists(fpath):
|
| 240 |
+
os.makedirs(fpath)
|
| 241 |
+
cv2.imwrite(os.path.join(fpath, filename), image)
|
| 242 |
+
|
| 243 |
+
self.db.add(
|
| 244 |
+
ids=[idx],
|
| 245 |
+
embeddings=[embedding['embedding']],
|
| 246 |
+
metadatas=[{'name': full_name}]
|
| 247 |
+
)
|
| 248 |
+
|
| 249 |
+
# Insert to mySQL
|
| 250 |
+
sql = "INSERT INTO log (id, name, scheck) VALUES (%s, %s, %s)"
|
| 251 |
+
val = (idx, full_name, '1')
|
| 252 |
+
command.execute(sql, val)
|
| 253 |
+
connection.commit()
|
| 254 |
+
|
| 255 |
+
print("Successfully registered face")
|
| 256 |
+
|
| 257 |
+
# query matched face and save to mysql
|
| 258 |
+
def verify(self):
|
| 259 |
+
frame = self.cap
|
| 260 |
+
|
| 261 |
+
self.cropped_frame = frame
|
| 262 |
+
|
| 263 |
+
if self.cropped_frame is not None and self.cropped_frame.size != 0:
|
| 264 |
+
try:
|
| 265 |
+
embedding = DeepFace.represent(img_path=self.cropped_frame, enforce_detection=False)[0]
|
| 266 |
+
print(len(embedding['embedding']))
|
| 267 |
+
results = self.db.query(
|
| 268 |
+
query_embeddings=[embedding['embedding']], # Replace with your unknown embeddings
|
| 269 |
+
n_results=1
|
| 270 |
+
)
|
| 271 |
+
print(2)
|
| 272 |
+
except: print(0)
|
| 273 |
+
else:
|
| 274 |
+
print(results)
|
| 275 |
+
if results['distances'][0] and results['distances'][0][0] < 0.30:
|
| 276 |
+
name = results['metadatas'][0][0].get('name')
|
| 277 |
+
self.auto_regis(name)
|
| 278 |
+
|
| 279 |
+
else:
|
| 280 |
+
uniquekey = str(uuid.uuid4())
|
| 281 |
+
|
| 282 |
+
sql = "INSERT INTO anonymous_no (name) VALUES (%s)"
|
| 283 |
+
val = (uniquekey,)
|
| 284 |
+
command.execute(sql, val)
|
| 285 |
+
connection.commit()
|
| 286 |
+
|
| 287 |
+
sql = "SELECT number FROM anonymous_no WHERE name = (%s)"
|
| 288 |
+
val = (uniquekey,)
|
| 289 |
+
command.execute(sql,val)
|
| 290 |
+
numid = command.fetchall()
|
| 291 |
+
print(numid)
|
| 292 |
+
sql = "UPDATE anonymous_no SET name = 'anonymous' WHERE number = %s"
|
| 293 |
+
command.execute(sql,numid[0])
|
| 294 |
+
connection.commit()
|
| 295 |
+
|
| 296 |
+
name = "anonymous" + str(numid[0][0])
|
| 297 |
+
print(name)
|
| 298 |
+
self.auto_regis(name,uniquekey)
|
| 299 |
+
|
| 300 |
+
|
| 301 |
+
if __name__ == '__main__':
|
| 302 |
+
|
| 303 |
+
# Initialize YOLO model
|
| 304 |
+
model = YOLO('best.onnx', task="detect")
|
| 305 |
+
|
| 306 |
+
# Detect adding picture
|
| 307 |
+
data = sys.stdin.buffer.read()
|
| 308 |
+
np_data = np.frombuffer(data, np.uint8)
|
| 309 |
+
image = cv2.imdecode(np_data, cv2.IMREAD_COLOR)
|
| 310 |
+
|
| 311 |
+
head_helmet_detect = list(model(image,conf=0.65,classes=[0,1]))
|
| 312 |
+
for result in head_helmet_detect[0].boxes.data.tolist():
|
| 313 |
+
hx1,hy1,hx2,hy2,hconf,hlabel = result
|
| 314 |
+
if hlabel == 0:
|
| 315 |
+
print(f"\n\n\n\n{hx1} {hx2} {hy1} {hy2}\n\n\n\n")
|
| 316 |
+
image1 = image[int(hy1):int(hy2), int(hx1):int(hx2), :]
|
| 317 |
+
|
| 318 |
+
|
| 319 |
+
time.sleep(0.5)
|
| 320 |
+
process = FaceDetection(capture=image1)
|
| 321 |
+
process.verify() # Run Detection from cropped image
|
| 322 |
+
# set path folder
|
| 323 |
+
# folder_to_monitor = "img"
|
| 324 |
+
|
| 325 |
+
# event_handler = ImageHandler()
|
| 326 |
+
# observer = Observer()
|
| 327 |
+
# observer.schedule(event_handler, path=folder_to_monitor, recursive=False)
|
| 328 |
+
|
| 329 |
+
# print(f"Monitoring folder: {folder_to_monitor}")
|
| 330 |
+
# observer.start()
|
| 331 |
+
|
| 332 |
+
# try:
|
| 333 |
+
# while True:
|
| 334 |
+
# time.sleep(1) # Keep the script running
|
| 335 |
+
# except KeyboardInterrupt:
|
| 336 |
+
# observer.stop()
|
| 337 |
+
|
| 338 |
+
# observer.join()
|
| 339 |
+
|
| 340 |
+
# Close MySQL connection
|
| 341 |
+
connection.close()
|
report.py
ADDED
|
@@ -0,0 +1,296 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import matplotlib.pyplot as plt
|
| 2 |
+
from fpdf import FPDF
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import uuid
|
| 5 |
+
|
| 6 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 7 |
+
from langchain_core.messages import HumanMessage
|
| 8 |
+
from langchain_ollama import ChatOllama
|
| 9 |
+
|
| 10 |
+
import matplotlib.pyplot as plt
|
| 11 |
+
|
| 12 |
+
import base64
|
| 13 |
+
from io import BytesIO
|
| 14 |
+
from PIL import Image
|
| 15 |
+
|
| 16 |
+
from datetime import datetime
|
| 17 |
+
import random
|
| 18 |
+
|
| 19 |
+
class ReportGenerator(FPDF):
|
| 20 |
+
def __init__(self, start_date, end_date):
|
| 21 |
+
super().__init__(unit='cm')
|
| 22 |
+
|
| 23 |
+
self.start_date = start_date
|
| 24 |
+
self.end_date = end_date
|
| 25 |
+
|
| 26 |
+
self.model = ChatOllama(model="llava-phi3:3.8b", temperature=0)
|
| 27 |
+
self.chain = self.prompt_func | self.model | StrOutputParser()
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def generate_months(self):
|
| 31 |
+
# Convert input strings to datetime objects
|
| 32 |
+
start = datetime.strptime(self.start_date, '%b %Y')
|
| 33 |
+
end = datetime.strptime(self.end_date, '%b %Y')
|
| 34 |
+
|
| 35 |
+
# Generate the list of month-year strings
|
| 36 |
+
months = []
|
| 37 |
+
current = start
|
| 38 |
+
while current <= end:
|
| 39 |
+
months.append(current.strftime('%b %Y'))
|
| 40 |
+
# Move to the next month
|
| 41 |
+
if current.month == 12:
|
| 42 |
+
current = current.replace(year=current.year + 1, month=1)
|
| 43 |
+
else:
|
| 44 |
+
current = current.replace(month=current.month + 1)
|
| 45 |
+
|
| 46 |
+
# Generate random numbers between 20 and 100 for each month
|
| 47 |
+
random_numbers = [random.randint(20, 100) for _ in range(len(months))]
|
| 48 |
+
|
| 49 |
+
return months, random_numbers
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def convert_to_base64(pil_image):
|
| 55 |
+
buffered = BytesIO()
|
| 56 |
+
pil_image.save(buffered, format="PNG") # You can change the format if needed
|
| 57 |
+
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 58 |
+
return img_str
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def create_graph_1(self):
|
| 62 |
+
# Sample data
|
| 63 |
+
x, y = self.generate_months()
|
| 64 |
+
|
| 65 |
+
# Create a plot
|
| 66 |
+
plt.figure(figsize=(10, 3)) # Adjust the figure size to fit the left space
|
| 67 |
+
plt.gcf().set_facecolor('#9edbff')
|
| 68 |
+
plt.plot(x, y, marker='s', color='#1b1464')
|
| 69 |
+
for i, label in enumerate(y):
|
| 70 |
+
plt.text(x[i], y[i] - 20, str(label) + "%", ha='center', va='bottom', fontsize=12, color='black')
|
| 71 |
+
|
| 72 |
+
plt.ylabel("Helmet Not Use, in %", fontsize=10, weight='bold')
|
| 73 |
+
plt.ylim(0, 100)
|
| 74 |
+
|
| 75 |
+
# Save the plot as an image
|
| 76 |
+
plt.savefig("./temp/graph_1.png", bbox_inches='tight') # Save with tight layout
|
| 77 |
+
plt.close()
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
def create_graph_2(self):
|
| 81 |
+
data = [30, 20, 24, 26] # Values for the pie chart
|
| 82 |
+
labels = ['Student', 'Passenger', 'Other', 'Family Member'] # Labels for the slices
|
| 83 |
+
|
| 84 |
+
fig, ax = plt.subplots(figsize=(4, 4), facecolor='#9edbff') # Set canvas background
|
| 85 |
+
#ax.set_facecolor('white') # Set the pie chart's background to white
|
| 86 |
+
|
| 87 |
+
colors = ['#C96868', '#FADFA1', '#FFF4EA', '#7EACB5']
|
| 88 |
+
|
| 89 |
+
# Draw the pie chart with labels inside
|
| 90 |
+
wedges, texts, autotexts = ax.pie(
|
| 91 |
+
data, labels=None, # Exclude outer labels
|
| 92 |
+
colors=colors,
|
| 93 |
+
autopct='%1.1f%%', startangle=90,
|
| 94 |
+
textprops=dict(color="black", fontsize=12, weight='bold') # Style the text inside
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
# Add original labels to the pie wedges
|
| 98 |
+
for i, autotext in enumerate(autotexts):
|
| 99 |
+
autotext.set_text(f"{labels[i]}\n{autotext.get_text()}") # Label + percentage
|
| 100 |
+
|
| 101 |
+
# Make sure the pie chart is circular
|
| 102 |
+
ax.axis('equal')
|
| 103 |
+
|
| 104 |
+
plt.savefig("./temp/graph_2.png", bbox_inches='tight') # Save the chart as an image
|
| 105 |
+
plt.close() # Close the plot to free memory
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
def bullet_multicell(self, width, height, bullet, text, bullet_indent=0.4):
|
| 110 |
+
# Add the bullet and first line of text
|
| 111 |
+
self.cell(bullet_indent, height, bullet) # Bullet with small space
|
| 112 |
+
self.multi_cell(width - bullet_indent, height, text)
|
| 113 |
+
|
| 114 |
+
# Adjust X position for wrapped lines (subsequent lines)
|
| 115 |
+
last_y = self.get_y()
|
| 116 |
+
self.set_xy(self.l_margin, last_y) # Indent wrapped lines
|
| 117 |
+
return last_y
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
def generate(self):
|
| 121 |
+
self.set_auto_page_break(auto=True, margin=1.5)
|
| 122 |
+
self.add_page()
|
| 123 |
+
|
| 124 |
+
primary_color = (56, 182, 255)
|
| 125 |
+
secondary_color = (27, 20, 100)
|
| 126 |
+
self.image('./resources/banner.png', x=0, y=0, w=21.0)
|
| 127 |
+
|
| 128 |
+
self.set_text_color(255, 255, 255)
|
| 129 |
+
|
| 130 |
+
self.set_xy(1.0, 1.23)
|
| 131 |
+
self.set_font('Arial', 'B', 24)
|
| 132 |
+
self.cell(0, 0, 'Motorcycle Helmet Safety', ln=False, align='L')
|
| 133 |
+
self.set_xy(1.0, 2.22)
|
| 134 |
+
self.set_font('Arial', '', 18)
|
| 135 |
+
self.cell(0, 0, 'Statistics Report', ln=False, align='L')
|
| 136 |
+
|
| 137 |
+
self.set_xy(17.22, 1.73)
|
| 138 |
+
self.set_font('Arial', '', 12)
|
| 139 |
+
self.cell(0, 0.5, 'This document', ln=True, align='L')
|
| 140 |
+
self.set_x(17.22)
|
| 141 |
+
self.cell(0, 0.5, 'was made', ln=True, align='L')
|
| 142 |
+
self.set_x(17.22)
|
| 143 |
+
self.cell(0, 0.5, 'by Knock Knock', ln=True, align='L')
|
| 144 |
+
|
| 145 |
+
self.set_draw_color(*primary_color)
|
| 146 |
+
self.set_line_width(0.2)
|
| 147 |
+
self.line(1.0 + 0.2, 3.39 - 0.2, 7.82, 3.39 - 0.2)
|
| 148 |
+
|
| 149 |
+
self.set_text_color(0, 0, 0)
|
| 150 |
+
|
| 151 |
+
date = f'{self.start_date} - {self.end_date}'
|
| 152 |
+
|
| 153 |
+
self.set_xy(1.0, 4.3)
|
| 154 |
+
self.set_font('Arial', '', 14)
|
| 155 |
+
self.cell(0, 0.6, str(uuid.uuid4()), ln=False, align='L')
|
| 156 |
+
self.cell(0, 0.6, date, ln=True, align='R')
|
| 157 |
+
|
| 158 |
+
self.ln(0.25)
|
| 159 |
+
|
| 160 |
+
self.set_font('Arial', 'b', 21)
|
| 161 |
+
self.cell(0, 1, f'Motorcycle Helmet Non-use', ln=True, align='L')
|
| 162 |
+
self.cell(0, 0.6, f'in {date} Overall Results', ln=True, align='L')
|
| 163 |
+
|
| 164 |
+
self.ln(0.75)
|
| 165 |
+
|
| 166 |
+
self.set_font('Arial', 'b', 18)
|
| 167 |
+
self.cell(0, 0, 'Camera Information', ln=True, align='L')
|
| 168 |
+
|
| 169 |
+
self.ln(0.5)
|
| 170 |
+
|
| 171 |
+
self.set_x(1.5)
|
| 172 |
+
self.set_font('Arial', 'b', 14)
|
| 173 |
+
self.multi_cell(19, 0.6, f"{'HP 4M Fixed USB Camera'}", align='L')
|
| 174 |
+
|
| 175 |
+
self.set_font('Arial', '', 14)
|
| 176 |
+
|
| 177 |
+
self.set_x(2.0)
|
| 178 |
+
self.bullet_multicell(19-2, 0.6, "-", f"IP: {'122.22.25.2'}")
|
| 179 |
+
|
| 180 |
+
self.set_x(2.0)
|
| 181 |
+
#self.multi_cell(19, 0.6, f"- Address: {'1 Chalong Krung 1 Alley, Lat Krabang, Khet Lat Krabang, Krung Thep Maha Nakhon 10520'}", align='J')
|
| 182 |
+
self.bullet_multicell(19-2, 0.6, "-", f"Address: {'1 Chalong Krung 1 Alley, Lat Krabang, Khet Lat Krabang, Krung Thep Maha Nakhon 10520'}")
|
| 183 |
+
|
| 184 |
+
self.set_x(2.0)
|
| 185 |
+
self.bullet_multicell(19-2, 0.6, "-", f"Administrator: {'KMITL'}")
|
| 186 |
+
|
| 187 |
+
|
| 188 |
+
self.ln(0.75)
|
| 189 |
+
|
| 190 |
+
self.set_font('Arial', 'b', 18)
|
| 191 |
+
self.cell(0, 0, 'Introduction', ln=True, align='L')
|
| 192 |
+
self.ln(0.5)
|
| 193 |
+
|
| 194 |
+
query = f"Write me 2-3 paragraphs about the introduction of Motorcycle Helmet Non-use less than 120 words"
|
| 195 |
+
|
| 196 |
+
introduction = " "*4 + self.chain.invoke({"text": query, "image": ''}).strip().replace("\t", "").replace("\n", "\n"+" "*4)
|
| 197 |
+
#summary = 3*(' '*4 + "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.\n\n")
|
| 198 |
+
|
| 199 |
+
self.set_font('Arial', '', 14)
|
| 200 |
+
self.multi_cell(19, 0.6, introduction, align='J')
|
| 201 |
+
|
| 202 |
+
self.set_draw_color(*secondary_color)
|
| 203 |
+
self.set_line_width(0.15)
|
| 204 |
+
self.line(1.0 + 0.15, 29 - 0.15, 20, 29 - 0.15)
|
| 205 |
+
|
| 206 |
+
|
| 207 |
+
self.add_page()
|
| 208 |
+
|
| 209 |
+
|
| 210 |
+
self.ln(0.25)
|
| 211 |
+
self.set_font('Arial', '', 14)
|
| 212 |
+
self.cell(0, 1.2, 'Figure 1', ln=True, align='L')
|
| 213 |
+
self.set_font('Arial', 'b', 18)
|
| 214 |
+
self.cell(0, 0, f'Motorcycle Helmet Non-use in {date}', ln=True, align='L')
|
| 215 |
+
|
| 216 |
+
self.ln(0.4)
|
| 217 |
+
self.create_graph_1()
|
| 218 |
+
self.image("./temp/graph_1.png", x=1, w=19, h=7)
|
| 219 |
+
|
| 220 |
+
self.ln(0.25)
|
| 221 |
+
query = f"Analyze the chart depicting the percentage of motorcycle helmet non-use for the month of {date}. Please include key insights such as trends over time, comparisons with previous months or years, any noticeable spikes or drops, and possible reasons for these changes. Summarize your analysis in 1 short paragraph. less than 60 words"
|
| 222 |
+
description_1 = " "*4 + self.chain.invoke({"text": query, "image": "./temp/graph_1.png"}).strip().replace("\t", "").replace("\n", "\n"+" "*4)
|
| 223 |
+
#description_1 = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
|
| 224 |
+
|
| 225 |
+
self.set_font('Arial', '', 14)
|
| 226 |
+
self.multi_cell(19, 0.6, description_1, align='J')
|
| 227 |
+
|
| 228 |
+
|
| 229 |
+
self.ln(0.1)
|
| 230 |
+
self.set_font('Arial', '', 14)
|
| 231 |
+
self.cell(0, 1.2, 'Figure 2', ln=True, align='L')
|
| 232 |
+
self.set_font('Arial', 'b', 18)
|
| 233 |
+
self.cell(0, 0, f'Motorcyclists, by Helmet Type', ln=True, align='L')
|
| 234 |
+
|
| 235 |
+
y = self.get_y()
|
| 236 |
+
|
| 237 |
+
self.ln(0.4)
|
| 238 |
+
self.create_graph_2()
|
| 239 |
+
self.image("./temp/graph_2.png", x=1, w=9, h=9)
|
| 240 |
+
|
| 241 |
+
|
| 242 |
+
query = f"Analyze the pie chart illustrating the proportion of different categories of individuals who are not wearing motorcycle helmets Discuss Consider potential implications for safety measures or awareness campaigns. Summarize your insights in 1 short paragraph less than 30 words."
|
| 243 |
+
description_2 = " "*4 + self.chain.invoke({"text": query, "image": "./temp/graph_2.png"}).strip().replace("\t", "").replace("\n", "\n"+" "*4)
|
| 244 |
+
#description_2 = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
|
| 245 |
+
|
| 246 |
+
self.set_font('Arial', '', 14)
|
| 247 |
+
self.set_xy(10.5, y + 0.5)
|
| 248 |
+
self.multi_cell(9.5, 0.6, description_2, align='J')
|
| 249 |
+
|
| 250 |
+
|
| 251 |
+
self.set_draw_color(*secondary_color)
|
| 252 |
+
self.set_line_width(0.15)
|
| 253 |
+
self.line(1.0 + 0.15, 29 - 0.15, 20, 29 - 0.15)
|
| 254 |
+
|
| 255 |
+
filename = f'./temp/motorcycle_helmet_report_{self.start_date}_{self.end_date}.pdf'
|
| 256 |
+
self.output(filename)
|
| 257 |
+
|
| 258 |
+
return filename
|
| 259 |
+
|
| 260 |
+
|
| 261 |
+
def prompt_func(self, data):
|
| 262 |
+
text = data["text"]
|
| 263 |
+
image = data["image"]
|
| 264 |
+
|
| 265 |
+
content_parts = []
|
| 266 |
+
|
| 267 |
+
text_part = {"type": "text", "text": text}
|
| 268 |
+
content_parts.append(text_part)
|
| 269 |
+
|
| 270 |
+
if image != '':
|
| 271 |
+
image_part = {
|
| 272 |
+
"type": "image_url",
|
| 273 |
+
"image_url": f"data:image/jpeg;base64,{image}",
|
| 274 |
+
}
|
| 275 |
+
content_parts.append(image_part)
|
| 276 |
+
|
| 277 |
+
return [HumanMessage(content=content_parts)]
|
| 278 |
+
|
| 279 |
+
|
| 280 |
+
def describe_image(self, text, image_path):
|
| 281 |
+
pil_image = Image.open(image_path)
|
| 282 |
+
image_b64 = self.convert_to_base64(pil_image)
|
| 283 |
+
|
| 284 |
+
query_chain = self.chain.invoke(
|
| 285 |
+
{"text": text, "image": image_b64}
|
| 286 |
+
)
|
| 287 |
+
|
| 288 |
+
return query_chain
|
| 289 |
+
|
| 290 |
+
|
| 291 |
+
if __name__ == '__main__':
|
| 292 |
+
report = ReportGenerator('NOV 2024', 'NOV 2025')
|
| 293 |
+
#report.generate()
|
| 294 |
+
x, y = report.generate_months()
|
| 295 |
+
print(x, y)
|
| 296 |
+
pass
|
requirements.txt
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fpdf == 1.7.2
|
| 2 |
+
Pillow == 9.5.0
|
| 3 |
+
gradio
|
| 4 |
+
opencv-python
|
| 5 |
+
numpy == 1.24.3
|
| 6 |
+
watchdog
|
| 7 |
+
ultralytics
|
| 8 |
+
deepface
|
| 9 |
+
chromadb
|
| 10 |
+
mysql-connector-python
|
| 11 |
+
matplotlib
|
| 12 |
+
pandas
|
| 13 |
+
langchain_core
|
| 14 |
+
langchain_ollama
|
| 15 |
+
tf-keras
|
| 16 |
+
onnx
|