-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from bencobley/charlie
Charlie
- Loading branch information
Showing
24 changed files
with
2,065 additions
and
322 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
from edgetpu.classification.engine import ClassificationEngine | ||
from edgetpu.utils import dataset_utils | ||
from PIL import Image | ||
from threading import Thread, Event | ||
from queue import Queue, Empty | ||
|
||
|
||
import logging | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class Classify(object): | ||
"""Save image to file""" | ||
|
||
def __init__(self): | ||
|
||
self.tests = { | ||
"pasta": { | ||
"labels": dataset_utils.read_label_file("models/pasta.txt"), | ||
"model": ClassificationEngine("models/pasta.tflite"), | ||
"threshold": 0.8, | ||
}, | ||
"sauce": { | ||
"labels": dataset_utils.read_label_file("models/sauce.txt"), | ||
"model": ClassificationEngine("models/sauce.tflite"), | ||
"threshold": 0.8, | ||
}, | ||
"pan_on_off": { | ||
"labels": dataset_utils.read_label_file("models/pan_on_off.txt"), | ||
"model": ClassificationEngine("models/pan_on_off.tflite"), | ||
"threshold": 0.5, | ||
}, | ||
} | ||
|
||
self.quit_event = Event() | ||
self.file_queue = Queue() | ||
self.data = None | ||
|
||
def _worker(self): | ||
|
||
logger.info("Initialising upload worker") | ||
|
||
while True: | ||
try: # Timeout raises queue.Empty | ||
|
||
image = self.file_queue.get(block=True, timeout=0.1) | ||
image = Image.open(image) | ||
|
||
output = {} | ||
|
||
for name, t in self.tests.items(): | ||
|
||
logger.info("Starting test %s " % (name)) | ||
|
||
engine = t["model"] | ||
labels = t["labels"] | ||
threshold = t["threshold"] | ||
|
||
result = engine.classify_with_image( | ||
image, top_k=1, threshold=threshold | ||
) | ||
logger.info(result) | ||
|
||
try: | ||
result = result[0] | ||
output[name] = { | ||
"label": labels[result[0]], | ||
"confidence": str(result[1]), | ||
} | ||
except TypeError: | ||
logger.info("TypeError") | ||
except IndexError: | ||
logger.info("IndexError") | ||
logger.info(output) | ||
self.data = output | ||
self.file_queue.task_done() | ||
|
||
except Empty: | ||
if self.quit_event.is_set(): | ||
logger.info("Quitting thread...") | ||
break | ||
|
||
def start(self, file_path): | ||
logger.info("Calling start") | ||
self.file_queue.put(file_path) | ||
|
||
def join(self): | ||
logger.info("Calling join") | ||
self.file_queue.join() | ||
|
||
def launch(self): | ||
logger.info("Initialising worker") | ||
self.thread = Thread(target=self._worker, daemon=True) | ||
self.thread.start() | ||
|
||
def quit(self): | ||
self.quit_event.set() | ||
logger.info("Waiting for classification thread to finish uploading") | ||
self.thread.join() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1 @@ | ||
{ | ||
"camera_sleep": 0, | ||
"Kp": 0.5, | ||
"Ki": 0, | ||
"Kd": 0, | ||
"sample_time": 0.01, | ||
"output_limit": 100 | ||
} | ||
{"frame_interval": "10", "Kp": 1.5, "Ki": 0.03, "Kd": 0.0, "sample_time": 0.01, "output_limit": 75} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.