forked from pythonlessons/TensorFlow-2.x-YOLOv3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fake-watch.py
72 lines (57 loc) · 2.28 KB
/
fake-watch.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
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
import cv2
import numpy as np
import tensorflow as tf
from yolov3.yolov4 import Create_Yolo
from yolov3.utils import load_yolo_weights, detect_image
from yolov3.configs import *
Darknet_weights = YOLO_V3_TINY_WEIGHTS if TRAIN_YOLO_TINY else YOLO_V3_WEIGHTS
print("Adding weights")
yolo = Create_Yolo(input_size=YOLO_INPUT_SIZE, CLASSES=TRAIN_CLASSES)
yolo.load_weights("./checkpoints/yolov3_custom") # use keras weights
print("ready to detect")
class Watcher:
DIRECTORY_TO_WATCH = "/home/pi/PiServer/Julio/machine_learning/watcher/"
def __init__(self):
self.observer = Observer()
def run(self):
event_handler = Handler()
self.observer.schedule(event_handler, self.DIRECTORY_TO_WATCH, recursive=True)
self.observer.start()
try:
while True:
time.sleep(5)
except:
self.observer.stop()
print("Error")
self.observer.join()
class Handler(FileSystemEventHandler):
@staticmethod
def on_any_event(event):
if event.is_directory:
return None
elif event.event_type == 'created':
# Take any action here when a file is first created.
print (event.src_path)
if "__" not in event.src_path:
# *****************************
# *****************************
print("Analysing image...")
detect_image(yolo, event.src_path, event.src_path, input_size=YOLO_INPUT_SIZE, show=False, CLASSES=TRAIN_CLASSES, rectangle_colors=(255,0,0))
print("Waiting for new image")
# ******************************
# ******************************
# elif event.event_type == 'modified':
# Taken any action here when a file is modified.
# print("Received modified event - %s." % event.src_path)
elif event.event_type == 'deleted':
# Taken any action here when a file is deleted.
if "__" not in event.src_path:
print("Received deleted event - %s." % event.src_path)
if __name__ == '__main__':
w = Watcher()
w.run()