Skip to content

Instantly share code, notes, and snippets.

@invokesus
Last active November 17, 2018 05:13
Show Gist options
  • Save invokesus/8613a8fdad6789edb3023eae3253c0fc to your computer and use it in GitHub Desktop.
Save invokesus/8613a8fdad6789edb3023eae3253c0fc to your computer and use it in GitHub Desktop.
import os.path
import time
from json import loads
from darkflow.net.build import TFNet
import cv2
options = {"model": "cfg/yolo.cfg", "load": "bin/yolo.weights", "threshold": 0.5, "gpu": 1.0}
tfnet = TFNet(options)
initial_file_name = 0
file_name = initial_file_name
seen_objects = set()
counter = 0
while True:
counter = (counter + 1) % 2
if True:
files = os.listdir("./images")
last_file = sorted(
[x.split(".")[0] for x in files], key=lambda x: int(x), reverse=True
)
if len(last_file) >0:
last_file = last_file[0]
last_file = int(last_file)
print("Inferencing file {}".format(last_file))
if last_file > file_name:
file_name = last_file -1
if os.path.isfile("./images/{}.png".format(file_name)):
imgcv = cv2.imread("./images/{}.png".format(file_name))
result = tfnet.return_predict(imgcv)
print(result)
parsed_result = result
if len(parsed_result) > 0:
temp = sorted(parsed_result, key=lambda x: x["confidence"], reverse=True)
if len(temp) > 2:
if temp[0]["label"] in seen_objects:
detected_object = temp[1]
else:
detected_object = temp[0]
else:
detected_object = temp[0]
if not detected_object["label"] in seen_objects:
with open("./label.txt", "w") as f:
f.write(str(detected_object["label"]))
seen_objects.add(detected_object["label"])
file_name += 5
else:
print("no file found")
time.sleep(0.1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment