-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpi_upload.py
59 lines (46 loc) · 1.4 KB
/
rpi_upload.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
import cv2 as cv
from datetime import datetime
from picamera2 import Picamera2, Preview
import requests
import sched
import sys
import time
picam2 = Picamera2()
current_id = 0
preview_config = picam2.create_preview_configuration(main={"size": (1280, 720)})
picam2.configure(preview_config)
def take_picture(scheduler):
global current_id
scheduler.enter(5, 1, take_picture, (scheduler,))
try:
time.sleep(2)
picam2.capture_file("current_frame.jpg")
image = cv.imread("current_frame.jpg")
image = cv.putText(
image,
datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
(50, 50),
cv.FONT_HERSHEY_SIMPLEX,
1,
(255, 255, 255),
2,
)
response = requests.post(
f"http://timelapse/inprogress/add?id={current_id}",
cv.imencode(".jpg", image)[1].tobytes(),
headers={"Content-Type": "image/jpeg"},
)
response.raise_for_status()
current_id = response.content.decode()
except Exception as e:
print(f"Failure during image upload: {e}")
def main():
global current_id
if len(sys.argv) > 1:
current_id = sys.argv[1]
picam2.start()
my_scheduler = sched.scheduler(time.time, time.sleep)
my_scheduler.enter(5, 1, take_picture, (my_scheduler,))
my_scheduler.run()
if __name__ == "__main__":
main()