-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathclip_application.py
71 lines (64 loc) · 2.38 KB
/
clip_application.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
import gi
gi.require_version('Gst', '1.0')
from gi.repository import Gst
import hailo
from clip_app.clip_app import ClipApp
class app_callback_class:
def __init__(self):
self.frame_count = 0
self.use_frame = False
self.running = True
def increment(self):
self.frame_count += 1
def get_count(self):
return self.frame_count
def app_callback(self, pad, info, user_data):
"""
This is the callback function that will be called when data is available
from the pipeline.
Processing time should be kept to a minimum in this function.
If longer processing is needed, consider using a separate thread / process.
"""
# Get the GstBuffer from the probe info
buffer = info.get_buffer()
# Check if the buffer is valid
if buffer is None:
return Gst.PadProbeReturn.OK
string_to_print = ""
# Get the detections from the buffer
roi = hailo.get_roi_from_buffer(buffer)
detections = roi.get_objects_typed(hailo.HAILO_DETECTION)
if len(detections) == 0:
detections = [roi] # Use the ROI as the detection
# Parse the detections
for detection in detections:
track = detection.get_objects_typed(hailo.HAILO_UNIQUE_ID)
track_id = None
label = None
confidence = 0.0
for track_id_obj in track:
track_id = track_id_obj.get_id()
if track_id is not None:
string_to_print += f'Track ID: {track_id} '
classifications = detection.get_objects_typed(hailo.HAILO_CLASSIFICATION)
if len(classifications) > 0:
string_to_print += ' CLIP Classifications:'
for classification in classifications:
label = classification.get_label()
confidence = classification.get_confidence()
string_to_print += f'Label: {label} Confidence: {confidence:.2f} '
string_to_print += '\n'
if isinstance(detection, hailo.HailoDetection):
label = detection.get_label()
bbox = detection.get_bbox()
confidence = detection.get_confidence()
string_to_print += f"Detection: {label} {confidence:.2f}\n"
if string_to_print:
print(string_to_print)
return Gst.PadProbeReturn.OK
def main():
user_data = app_callback_class()
clip = ClipApp(user_data, app_callback)
clip.run()
if __name__ == "__main__":
main()