Skip to content

Commit

Permalink
Merge pull request #24 from jdpdev/feature-#17-save-birbvision-evalua…
Browse files Browse the repository at this point in the history
…tion

Feature #17 save birbvision evaluation
  • Loading branch information
jdpdev authored Apr 16, 2021
2 parents 85f46e6 + 53ef4e2 commit c254cbc
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 3 deletions.
8 changes: 6 additions & 2 deletions birbcam/birbwatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from datetime import datetime
from setproctitle import setproctitle
from birbvision import ClassifyBird
from .picturelogger import PictureLogger

from birbcam.picturetaker import PictureTaker, filename_filestamp, filename_live_picture
from .birbconfig import BirbConfig
Expand Down Expand Up @@ -60,6 +61,7 @@ def __init__(self, config: BirbConfig):
self.pauseRecording = True

self.classifier = ClassifyBird()
self.pictureLogger = PictureLogger(f"{config.saveTo}/pictures.txt")

def run(self, camera, mask):
camera.shutter_speed = self.shutterFlipper.value
Expand Down Expand Up @@ -105,7 +107,9 @@ def __loop(self, camera, rawCapture, mask):
#logging.info(f"[birbvision] shoot: {classifyResults[0].label} @ {classifyResults[0].confidence:.2f}")
didTakeFullPicture = self.fullPictureTaker.take_picture(camera)
if didTakeFullPicture[0]:
cv2.imwrite(f"{self.config.saveTo}/thumb/{didTakeFullPicture[1]}", now)
thumbfile = f"{self.config.saveTo}/thumb/{didTakeFullPicture[1]}"
cv2.imwrite(thumbfile, now)
self.pictureLogger.log_picture(didTakeFullPicture[2], thumbfile, classifyResults, self.shutterFlipper.label, self.isoFlipper.label)
else:
# logging.info(f"[birbvision] ignore: {classifyResults[0].label} @ {classifyResults[0].confidence:.2f}")
self.fullPictureTaker.reset_time()
Expand All @@ -124,7 +128,7 @@ def __loop(self, camera, rawCapture, mask):
return False

def __classify_image(self, image):
classify = list(self.classifier.classify_image(image))
classify = self.classifier.classify_image(image)
results = classify.get_top_results(5)
return (results[0].label != "None" and results[0].confidence > 0.30, results)

Expand Down
1 change: 1 addition & 0 deletions birbcam/picturelogger/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .picturelogger import PictureLogger
74 changes: 74 additions & 0 deletions birbcam/picturelogger/picturelogger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import logging
from time import time

class PictureLogger:
def __init__(self, file_source):
self._loggedPictures = []
#self.__read_picture_history(file_source)

self._log_file = open(file_source, mode="a", encoding="utf-8")

def __del__(self):
self._log_file.close()

def log_picture(self, fullPath, thumbPath, classification, shutter, iso):
entry = {
"full": fullPath,
"thumb": thumbPath,
"evaluation": [dictify_classification(c) for c in classification],
"time": time(),
"shutter": shutter,
"iso": iso
}

self.__append_to_file(entry)

def __read_picture_history(self, file_source):
f = open(file_source, mode="r", encoding="utf-8")
for line in f:
self._loggedPictures.append(self.__deserialize_entry(line))

def __append_to_file(self, entry):
self._loggedPictures.append(entry)
self._log_file.write(self.__serialize_entry(entry))
self._log_file.write("\n")
self._log_file.flush()

def __serialize_entry(self, entry):
c = self.__serialize_classification(entry["evaluation"])
return f"{entry['time']}|{entry['full']}|{entry['thumb']}|{c}|{entry['shutter']}|{entry['iso']}"

def __deserialize_entry(self, string):
split = string.split("|")
return {
"time": split[0],
"full": split[1],
"thumb": split[2],
"evaluation": self.__deserialize_classification(split[3]),
"shutter": split[4],
"iso": split[5]
}

def __serialize_classification(self, results):
strings = [stringify_result(r) for r in results]
return "@".join(strings)

def __deserialize_classification(self, string):
split = string.split("@")
return [destringify_result(s) for s in split]

def dictify_classification(classification):
return {
"label": classification.label,
"confidence": classification.confidence
}

def stringify_result(result):
return f"{result['label']}~{result['confidence']}"

def destringify_result(result):
split = result.split("~")
return {
"label": split[0],
"confidence": split[1]
}
2 changes: 1 addition & 1 deletion birbcam/picturetaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def take_picture(self, camera):
camera.resolution = restoreResolution

self.__schedule_next_picture()
return (True, filename)
return (True, filename, filepath)

def __save_path(self, name = None):
if name == None:
Expand Down

0 comments on commit c254cbc

Please sign in to comment.