-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcatFinderMediaPipe.py
70 lines (55 loc) · 2.48 KB
/
catFinderMediaPipe.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
#!/usr/bin/env python
import cv2
import catFinder
from PIL import Image
import numpy as np
import mediapipe as mp
from mediapipe.tasks import python
from mediapipe.tasks.python import vision
import imgUtils
class CatFinderMediaPipe(catFinder.CatFinder):
def __init__(self, settingsObj, debug):
''' Initialise the MediaPipe tflite based cat finding object detector.
It expects the following elements in settingsObj:
- weights - filename of tflite weights file (e.g. model.tflite)
- thresholds - the threshold 0 to 1 to be used to determine if one of the model class is detected (e.g. 0.5 = 50% confidence)
'''
super().__init__(settingsObj, debug)
self.weightsFname = settingsObj['weights']
self.thresholds = settingsObj['thresholds']
BaseOptions = mp.tasks.BaseOptions
ObjectDetector = mp.tasks.vision.ObjectDetector
ObjectDetectorOptions = mp.tasks.vision.ObjectDetectorOptions
VisionRunningMode = mp.tasks.vision.RunningMode
options = ObjectDetectorOptions(
base_options=BaseOptions(model_asset_path=self.weightsFname),
max_results=5,
running_mode=VisionRunningMode.IMAGE)
self.model = ObjectDetector.create_from_options(options)
def getInferenceResults(self, img):
#print("catFinderMediaPipe.getInferenceResults() - img.shape=", img.shape)
mp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=img)
resultsObj = self.model.detect(mp_image)
#print("resultsObj=",resultsObj)
#print(dir(resultsObj))
retObj = {}
retObj['predictions'] = []
for r in resultsObj.detections:
#print(r)
#namesObj = r.names
#print("getInferenceResults() namesObj=", namesObj)
detObj = { 'class': None, 'confidence': 0 }
bbox = r.bounding_box
classObj = r.categories[0]
#print(bbox, classObj)
detObj['class'] = classObj.category_name
detObj['confidence'] = classObj.score
detObj['x'] = int(bbox.origin_x+0.5*bbox.width)
detObj['y'] = int(bbox.origin_y+0.5*bbox.height)
detObj['width'] = bbox.width
detObj['height'] = bbox.height
retObj['predictions'].append(detObj)
#print("CatFinderMediaPipe.getInferenceResults() - retObj=", retObj)
return retObj
if __name__ == "__main__":
print("CatFinderYolo.main()");