This repository has been archived by the owner on Jul 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_downloader.py
65 lines (57 loc) · 1.65 KB
/
image_downloader.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
import os
import sys
sys.path.append('/usr/local/lib/python2.7/site-packages')
import cv2
import numpy
import requests
import socket
from time import sleep
from datetime import datetime
from image_processor import preprocess_image, process_image
s = requests.Session()
s.auth = (
os.environ.get('CAMERA_USERNAME', 'admin'),
os.environ.get('CAMERA_PASSWORD', '')
)
img_url = os.environ.get(
'CAMERA_IMAGE_URL',
'http://192.168.179.4/cgi/jpg/image.cgi'
)
from server import db, Measurement
SUCCESS_INTERVAL = int(os.environ.get('SUCCESS_INTERVAL', '30'))
FAILURE_INTERVAL = int(os.environ.get('FAILURE_INTERVAL', '2'))
def send_measurement(value):
db.session.add(Measurement(
value=value,
timestamp=datetime.now()
))
db.session.commit()
if __name__ == "__main__":
window = '-window' in sys.argv
while(True):
try:
response = s.get(img_url, timeout=5)
except (
requests.exceptions.Timeout,
requests.exceptions.ConnectionError,
socket.timeout
):
print("Timeout")
sleep(1)
continue
if not response.ok:
print(response)
continue
buf = numpy.frombuffer(response.content, dtype="int8")
img = cv2.imdecode(buf, cv2.IMREAD_COLOR)
img = preprocess_image(img)
img, value = process_image(img, 'memory/memory.jpg')
if window:
cv2.imshow('image', img)
if value is not None:
send_measurement(value)
sleep(SUCCESS_INTERVAL)
else:
sleep(FAILURE_INTERVAL)
if window:
cv2.destroyAllWindows()