-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwatch_cameras.py
100 lines (75 loc) · 2.23 KB
/
watch_cameras.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import argparse
import json
import os
import queue
import re
import threading
import time
from datetime import timedelta
import PriusImageCache
import numpy as np
import requests
from PriusImageCache import ImageDeduplication
from timeloop import Timeloop
tl = Timeloop()
cams = []
cam_threads = []
dedup = ImageDeduplication()
q = queue.Queue()
ap = argparse.ArgumentParser()
ap.add_argument("-t", "--timer", required=False,
help="path to input image")
ap.add_argument("-c", "--cams", default='min_cams2.json',
help="base path to YOLO directory")
args = vars(ap.parse_args())
def watch_camera():
while True:
try:
cam = q.get()
if cam is None:
break
now = time.localtime()
frame_folder = str(now.tm_year) + str(now.tm_mon) + str(now.tm_mday) + "_" + str(
now.tm_hour) + "-" + str(now.tm_min) + "/"
frame_file = str(now.tm_year) + str(now.tm_mon) + str(now.tm_mday) + "_" + str(
now.tm_hour) + "-" + str(now.tm_min) + "_" + str(cam['id']) + ".jpg"
frame_dir = pathFolder + frame_folder
if os.path.exists(frame_dir) is False:
os.mkdir(frame_dir)
path = frame_dir + frame_file
img_data = requests.get(cam['url']).content
if dedup.is_image_duplicate(img_data, cam['id']):
print(str(frame_file) + " is a duplicate image. Removing.")
if os.path.exists(path):
os.remove(path)
else:
# Update hash
dedup.put_hash(img_data, cam['id'])
# Save File
with open(path, 'wb') as handler:
handler.write(img_data)
except Exception as e:
print(e)
cam = None
result = None
@tl.job(interval=timedelta(seconds=int(args["timer"])))
def watch_redmond_cameras_timer():
print("Cameras job current time : {}".format(time.ctime()))
for cam in cams:
q.put(cam)
def start_watching():
procs = 30
for i in range(0, procs):
process = threading.Thread(target=watch_camera)
cam_threads.append(process)
for t in cam_threads:
t.start()
if __name__ == '__main__':
print("Loading Seattle Cams")
with open(args["cams"], "r") as read_file:
cams = json.load(read_file)
for cam in cams:
q.put(cam)
pathFolder = "./frames/"
tl.start()
start_watching()