-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyze_pi.py
65 lines (57 loc) · 2.77 KB
/
analyze_pi.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
#!/usr/bin/python
import os
import subprocess
import glob
import queue_util
from global_constants import GlobalConstants
import sys
import psutil
import s3_util
import parse
import time
const = GlobalConstants()
analysis_dir = os.path.expanduser("~/pi-eye-py/pi_videos/")
result_dir = os.path.expanduser("~/pi-eye-py/pi_results/")
output_dir = os.path.expanduser("~/pi-eye-py/pi_outputs/")
processed_dir = os.path.expanduser("~/pi-eye-py/processed_videos/")
if not os.path.exists(result_dir):
subprocess.call(['mkdir',result_dir])
if not os.path.exists(analysis_dir):
subprocess.call(['mkdir',analysis_dir])
if not os.path.exists(output_dir):
subprocess.call(['mkdir',output_dir])
if __name__ == '__main__':
os.chdir(os.path.expanduser("~/darknet"))
try:
while True:
if (psutil.cpu_percent(interval=1) <= 85):
if os.listdir(analysis_dir):
start = time.time()
list_of_files = glob.glob(analysis_dir+'/*.h264')
print("\nlist_of_files: ",list_of_files)
latest_subdir = os.path.abspath(min(list_of_files, key=os.path.getmtime))
filename = (os.path.basename(latest_subdir))
print("\nProcessing Video File: \n",latest_subdir)
command = "./darknet detector demo cfg/coco.data cfg/yolov3-tiny.cfg yolov3-tiny.weights {0}".format(latest_subdir)
proc = subprocess.Popen([command], stdout=subprocess.PIPE, shell=True)
(out, err) = proc.communicate()
print("\nTime taken to analyze video {} is {} \n".format(filename,time.time()-start))
# result_file = os.path.join(result_dir,str(os.path.basename(latest_subdir)[:-5] + '_result.txt'))
# with open(result_file, 'w+') as fout:
# fout.write(str(parse.parse_result(out)))
# output_file = result_file.replace('_result','_output')
# with open(output_file, 'w+') as fout:
# fout.write(str(out))
result_body = parse.parse_result(out)
s3_util.upload_results(filename,result_body)
print("\nUploaded Result File: {} , detected: {} \n".format(filename,result_body))
# subprocess.run((' ').join(['mv',latest_subdir,processed_dir]),shell=True, check=True)
subprocess.run((' ').join(['rm', '-rf', latest_subdir]),shell=True, check=True)
else:
time.sleep(2)
print("\nPolling pi_videos directory")
except KeyboardInterrupt:
print("Quitting the program.")
except:
print("Unexpected error:" + str(sys.exc_info()[0]))
raise