-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcapture.py
76 lines (60 loc) · 2.44 KB
/
capture.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import cv2
import requests
import numpy as np
from threading import Thread
import time
from datetime import datetime
# Constants
WEBCAM_INDEX = 0 # Index of your USB webcam, typically 0
FPS = 5 # Frames per second for capturing
POST_URL = 'http://athene.fi:3333/upload' # URL to which images will be POSTed
class ImageCapture:
def __init__(self):
self.capture = cv2.VideoCapture(WEBCAM_INDEX)
self.capture.set(cv2.CAP_PROP_FPS, FPS)
self.is_capturing = False
self.CIRCLE_COUNT = True
def start_capture(self):
self.is_capturing = True
self.capture_thread = Thread(target=self._capture_loop)
self.capture_thread.start()
def stop_capture(self):
self.is_capturing = False
self.capture_thread.join()
def _capture_loop(self):
while self.is_capturing:
ret, frame = self.capture.read()
if ret:
self._send_frame(frame)
else:
print("Failed to capture frame.")
time.sleep(1/FPS)
def _send_frame(self, og_frame):
frame = cv2.resize(og_frame, (640, 480))
cv2.putText(frame, "SUB-CAM-1", (10, frame.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1, cv2.LINE_AA)
current_date = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
cv2.putText(frame, current_date, (frame.shape[1] - 200, frame.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1, cv2.LINE_AA)
rec_text = "REC"
cv2.putText(frame, rec_text, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 2, cv2.LINE_AA)
if self.CIRCLE_COUNT < 2:
cv2.circle(frame, (90, 20), 13, (0, 0, 255), -1) # Draw a red filled circle (ball)
self.CIRCLE_COUNT += 1
elif self.CIRCLE_COUNT < 4:
self.CIRCLE_COUNT += 1
else:
self.CIRCLE_COUNT = 0
_, img_encoded = cv2.imencode('.jpg', frame)
try:
response = requests.post(POST_URL, files={'image': ('image.jpg', img_encoded.tostring())})
if response.status_code != 200:
print("Failed to send frame to server.")
except Exception as e:
print("Error:", e)
if __name__ == "__main__":
try:
capture = ImageCapture()
capture.start_capture()
input("Press Enter to stop capture.")
capture.stop_capture()
except KeyboardInterrupt:
capture.stop_capture()