-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSource-Code
More file actions
126 lines (97 loc) · 3.49 KB
/
Copy pathSource-Code
File metadata and controls
126 lines (97 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import cv2
import requests
import time
import os
import sys
os.environ['DISPLAY'] = ':0'
SERVER_URL = "https://www.circuitdigest.cloud/api/v1/object-detection/detect"
API_KEY = "YourApikey"
CLASSES = "[]"
CONFIDENCE = "0.2"
MODE = "auto"
AUTO_INTERVAL = 5
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
if not cap.isOpened():
print("Camera not found! Check USB camera connection.")
sys.exit()
print("Camera initialized.")
print(f"Running in [{MODE}] mode")
if MODE == "keyboard":
print("Press SPACE to capture | Press ESC to quit")
elif MODE == "auto":
print(f"Auto capturing every {AUTO_INTERVAL} seconds | Press ESC to quit")
elif MODE == "ssh":
print("Auto capturing every 5 seconds | Press Ctrl+C to quit")
def send_image_to_api(frame):
for _ in range(3):
cap.read()
ret, frame = cap.read()
if not ret:
print("Capture failed")
return
_, img_encoded = cv2.imencode('.jpg', frame, [cv2.IMWRITE_JPEG_QUALITY, 90])
img_bytes = img_encoded.tobytes()
headers = { "X-API-Key": API_KEY }
files = { "imageFile": ("photo.jpg", img_bytes, "image/jpeg") }
data = { "classes": CLASSES, "confidence": CONFIDENCE }
try:
print("\nSending to Object Detection API...")
response = requests.post(SERVER_URL, headers=headers,
files=files, data=data, timeout=15)
if response.status_code == 200:
result = response.json()
# ? Fixed encoding issue
safe_response = response.text.encode('utf-8', errors='replace').decode('utf-8')
print("Response:", safe_response)
count = result.get("detection_count", 0)
print(f"Objects detected: {count}")
for det in result.get("detections", []):
label = det.get("class_name") or det.get("class", "unknown")
conf = det.get("confidence", 0)
# ? Fixed encoding for label too
safe_label = str(label).encode('utf-8', errors='replace').decode('utf-8')
print(f" - {safe_label} ({conf:.2f}%)")
else:
print(f"HTTP error: {response.status_code}")
except requests.exceptions.Timeout:
print("Request timed out!")
except Exception as e:
print(f"Error: {str(e).encode('utf-8', errors='replace').decode('utf-8')}")
if MODE == "ssh":
try:
while True:
ret, frame = cap.read()
if ret:
print("\nAuto capturing...")
send_image_to_api(frame)
time.sleep(AUTO_INTERVAL)
except KeyboardInterrupt:
print("Stopped by user.")
finally:
cap.release()
else:
last_capture_time = 0
while True:
ret, frame = cap.read()
if not ret:
print("Failed to grab frame")
break
cv2.imshow("Camera - SPACE: capture | ESC: quit", frame)
key = cv2.waitKey(1) & 0xFF
if MODE == "keyboard":
if key == 32:
print("\nCapturing image...")
send_image_to_api(frame)
elif MODE == "auto":
current_time = time.time()
if current_time - last_capture_time >= AUTO_INTERVAL:
last_capture_time = current_time
print("\nAuto capturing...")
send_image_to_api(frame)
if key == 27:
print("Quitting...")
break
cap.release()
cv2.destroyAllWindows()