Technical Details
Built with standard, powerful tools for efficient local processing on accessible hardware.
-
OS: Windows 10+ or Linux
-
Processor: Intel i3+ / AMD Ryzen 3+
-
RAM: 4GB+ (8GB Recommended)
-
Storage: 5GB Free Space
-
Language: Python 3.8+
-
Core Lib: OpenCV
-
AI Model: YOLOv8
-
GPU: Optional
(CUDA/ROCm)
import cv2
import math
import cvzone
import os
from ultralytics import YOLO
import time
import requests
API_ENDPOINT = "https://civiceye.my/api/log_violation"
AUTH_TOKEN = "YOUR_API_TOKEN_HERE"
DEVICE_ID = "CAM_001"
media_input = input("Enter video path or leave blank for camera: ").strip()
cap = cv2.VideoCapture(0) if not media_input else cv2.VideoCapture(media_input)
model = YOLO("Weights/best.pt")
classNames = ['With Helmet', 'Without Helmet']
def upload_violation(image_data, timestamp, frame_num):
try:
files = {'evidence_image': (f'{DEVICE_ID}_{timestamp}.jpg', image_data, 'image/jpeg')}
payload = {
'device_id': DEVICE_ID,
'timestamp': timestamp,
'frame_number': frame_num,
'violation_type': 'Without Helmet'
}
headers = {'Authorization': f'Bearer {AUTH_TOKEN}'}
response = requests.post(API_ENDPOINT, headers=headers, data=payload, files=files, timeout=10)
if response.status_code == 200:
print(f"Successfully uploaded violation for frame {frame_num}")
else:
print(f"Failed to upload violation: {response.status_code} - {response.text}")
except requests.exceptions.RequestException as e:
print(f"Network error during upload: {e}")
except Exception as e:
print(f"An error occurred during upload: {e}")
frame_count = 0
while True:
success, img = cap.read()
if not success: break
results = model(img, stream=True)
violation_detected_in_frame = False
for r in results:
for box in r.boxes:
x1, y1, x2, y2 = map(int, box.xyxy[0])
conf = math.ceil((box.conf[0] * 100)) / 100
cls = int(box.cls[0])
currentClass = classNames[cls]
box_color = (0, 255, 0) if currentClass == "With Helmet" else (0, 0, 255)
cvzone.cornerRect(img, (x1, y1, x2 - x1, y2 - y1), colorR=box_color)
cvzone.putTextRect(img, f'{currentClass} {conf:.2f}',
(max(0, x1), max(35, y1)),
scale=1, thickness=1, colorR=box_color)
if currentClass == "Without Helmet":
violation_detected_in_frame = True
if violation_detected_in_frame:
timestamp_str = time.strftime("%Y-%m-%dT%H:%M:%S")
_, buffer = cv2.imencode('.jpg', img)
image_bytes = buffer.tobytes()
upload_violation(image_bytes, timestamp_str, frame_count)
cvzone.putTextRect(img, "Violation Logged", (10, 30), scale=1, thickness=1, colorR=(0,0,255))
frame_count += 1
cv2.imshow("Civic Eye Detection", img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
print("Detection finished.")