Spaces:
Sleeping
Sleeping
jagdish-datanova
commited on
Commit
·
b60b6c5
1
Parent(s):
51eb5d7
updated code
Browse files- .gitignore +2 -0
- app.py +67 -0
- helper.py +67 -0
- prompts_and_chema.py +88 -0
- requirements.txt +0 -0
- text_files/001_quick_start.txt +156 -0
- text_files/002_yolov8.txt +51 -0
- text_files/007_model_pipelining.txt +54 -0
- text_files/009_zone_counting.txt +64 -0
- text_files/013_overlay_effects.txt +238 -0
- text_files/014_person_reid.txt +182 -0
- text_files/016_custom_video_source.txt +256 -0
- text_files/object_detection_class_filtering.txt +87 -0
- text_files/object_detection_image.txt +71 -0
- text_files/object_detection_video_stream.txt +75 -0
.gitignore
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
venv
|
2 |
+
__pycache__
|
app.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from helper import *
|
3 |
+
from prompts_and_chema import *
|
4 |
+
|
5 |
+
def full_chat_pipeline(user_query, openai_key):
|
6 |
+
try:
|
7 |
+
# ✅ Step 1: Set the OpenAI API key
|
8 |
+
openai.api_key = openai_key
|
9 |
+
|
10 |
+
# Step 1: Get intent from first GPT call
|
11 |
+
raw_response = chat_with_gpt(intent_system_prompt, user_query, use_schema=True, schema=intent_output_schema )
|
12 |
+
# print(f"raw_response: {raw_response}")
|
13 |
+
|
14 |
+
intents = json.loads(raw_response) if isinstance(raw_response, str) else raw_response
|
15 |
+
# print(f"Intents: {intents}")
|
16 |
+
|
17 |
+
# Step 2: Format prompt with detected intent
|
18 |
+
loaded_texts = load_intent_texts(intents, get_txt_files)
|
19 |
+
selected_intents_str = ', '.join(intents.get("intents", []))
|
20 |
+
context_str = '\n\n'.join(f"{k}:\n{v}" for k, v in loaded_texts.items())
|
21 |
+
formatted_prompt = f'''
|
22 |
+
|
23 |
+
User query: {user_query}
|
24 |
+
|
25 |
+
Selected Intents: {selected_intents_str}
|
26 |
+
|
27 |
+
Context: {context_str}
|
28 |
+
|
29 |
+
'''
|
30 |
+
|
31 |
+
# Step 3: Get final answer from second GPT call
|
32 |
+
final_response = chat_with_gpt(get_answer_system_prompt, formatted_prompt)
|
33 |
+
# print("Final Response:", final_response)
|
34 |
+
|
35 |
+
return f"{selected_intents_str}", f"{final_response}"
|
36 |
+
|
37 |
+
except Exception as e:
|
38 |
+
return f"Error while classifying intents: {str(e)}", f"Error while generating response: {str(e)}"
|
39 |
+
|
40 |
+
|
41 |
+
# Gradio UI
|
42 |
+
with gr.Blocks(title="Degirum LLM") as demo:
|
43 |
+
gr.Markdown("## Degirum LLM")
|
44 |
+
gr.Markdown("Ask questions...")
|
45 |
+
# 🔑 OpenAI Key on top
|
46 |
+
openai_key = gr.Textbox(label="🔑 OpenAI API Key", placeholder="Enter your OpenAI API key...", type="password")
|
47 |
+
|
48 |
+
# Row with query input and intent output side by side
|
49 |
+
with gr.Row():
|
50 |
+
user_query = gr.Textbox(label="💬 Query", placeholder="Type your question here...", lines=3)
|
51 |
+
intent_output = gr.Textbox(label="🧠 Classified Intents", placeholder="will get answer by AI", lines=3, interactive=False)
|
52 |
+
|
53 |
+
# 🚀 Submit Button
|
54 |
+
submit_btn = gr.Button("🚀 Submit", variant="primary")
|
55 |
+
|
56 |
+
# 🤖 AI Response at the bottom
|
57 |
+
response_output = gr.Textbox(label="🤖 Full AI Response", lines=10, interactive=False)
|
58 |
+
|
59 |
+
submit_btn.click(
|
60 |
+
fn=full_chat_pipeline,
|
61 |
+
inputs=[user_query, openai_key],
|
62 |
+
outputs=[intent_output, response_output]
|
63 |
+
)
|
64 |
+
|
65 |
+
|
66 |
+
if __name__ == "__main__":
|
67 |
+
demo.launch()
|
helper.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import openai
|
2 |
+
import os
|
3 |
+
import json
|
4 |
+
|
5 |
+
def chat_with_gpt(system_prompt, user_query, use_schema=False, schema=None):
|
6 |
+
"""
|
7 |
+
Function to interact with OpenAI's GPT model
|
8 |
+
|
9 |
+
Args:
|
10 |
+
system_prompt (str): The system prompt to set the context
|
11 |
+
user_query (str): The user's question or prompt
|
12 |
+
|
13 |
+
Returns:
|
14 |
+
str: The model's response
|
15 |
+
"""
|
16 |
+
try:
|
17 |
+
if use_schema:
|
18 |
+
response = openai.ChatCompletion.create(
|
19 |
+
model="gpt-4o",
|
20 |
+
messages=[
|
21 |
+
{"role": "system", "content": system_prompt},
|
22 |
+
{"role": "user", "content": user_query}
|
23 |
+
],
|
24 |
+
response_format={
|
25 |
+
"type": "json_schema",
|
26 |
+
"json_schema": {
|
27 |
+
"name": "classify-intents",
|
28 |
+
"schema": schema,
|
29 |
+
"strict": True}
|
30 |
+
},
|
31 |
+
)
|
32 |
+
return response.choices[0].message.content
|
33 |
+
else:
|
34 |
+
response = openai.ChatCompletion.create(
|
35 |
+
model="gpt-4o",
|
36 |
+
temperature = 0.6,
|
37 |
+
messages=[
|
38 |
+
{"role": "system", "content": system_prompt},
|
39 |
+
{"role": "user", "content": user_query}
|
40 |
+
],
|
41 |
+
)
|
42 |
+
return response.choices[0].message.content
|
43 |
+
except Exception as e:
|
44 |
+
return f"An error occurred: {str(e)}"
|
45 |
+
|
46 |
+
def load_intent_texts(intents, get_txt_files):
|
47 |
+
"""
|
48 |
+
Load text content for a list of intents based on a mapping of intent to file names.
|
49 |
+
|
50 |
+
Args:
|
51 |
+
intent_list (list): List of detected intents.
|
52 |
+
get_txt_files (dict): Mapping of intent names to file paths.
|
53 |
+
|
54 |
+
Returns:
|
55 |
+
dict: Dictionary with intent names as keys and file content as values.
|
56 |
+
"""
|
57 |
+
loaded_texts = {}
|
58 |
+
intent_list = intents.get("intents", [])
|
59 |
+
for intent in intent_list:
|
60 |
+
filename = get_txt_files.get(intent)
|
61 |
+
if filename:
|
62 |
+
try:
|
63 |
+
with open(filename, "r") as f:
|
64 |
+
loaded_texts[intent] = f.read()
|
65 |
+
except FileNotFoundError:
|
66 |
+
loaded_texts[intent] = f"[ERROR] File '{filename}' not found."
|
67 |
+
return loaded_texts
|
prompts_and_chema.py
ADDED
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
intent_output_schema = {
|
2 |
+
"type": "object",
|
3 |
+
"properties": {
|
4 |
+
"intents": {
|
5 |
+
"type": "array",
|
6 |
+
"items": {
|
7 |
+
"type": "string",
|
8 |
+
"enum": ["hello_world_of_pysdk", "single_model_inference", "running_yolo_models", "model_pipelining", "class_filtering", "overlay_effects", "running_inference_on_video", "person_re_identification_or_extracting_embeddings", "zone_counting", "custom_video_source", "not_supported"]
|
9 |
+
},
|
10 |
+
}
|
11 |
+
},
|
12 |
+
"required": ["intents"],
|
13 |
+
"additionalProperties": False
|
14 |
+
}
|
15 |
+
|
16 |
+
intent_system_prompt = '''
|
17 |
+
Degirum PySDK is a python based SDK that is built to support AI inference of computer vision models on multiple AI accelerators like Intel Openvino, Hailo, Memryx, N2X etc. The software stack provides unified way to run inference on any model across our model zoo or from a local model zoo and perform post processing over it.
|
18 |
+
|
19 |
+
I will provide you with a user query and your job is to classify the intent, the query belongs to. I have 10 intents. Each intent comprises of a feature or function that degirum supports.
|
20 |
+
|
21 |
+
Intents:
|
22 |
+
|
23 |
+
1. hello_world_of_pysdk: Basic setup, setting up account on DeGirum AI hub, generate dg_token, and run a simple example where you load a model from degirum model zoo, and just pass an example image and see output. Any question asked about setup, what is PySDK, generating tokens or starting with pysdk, a beginner friendly example, then select this intent.
|
24 |
+
|
25 |
+
2. single_model_inference: Running an image on a single model either from degirum AI Hub model zoo or local model zoo. A basic example where you pass an image to the model and get results. It can be a segmentation, object detection, classification or pose estimation model and result return type will be different for each. Any question on simple example to run a single AI model on images only, choosing inference device, display and printing results etc should fall under this intent.
|
26 |
+
|
27 |
+
3. running_yolo_models: This intent includes running different flavours of yolo models like yolo v5, v8, v11 - object detection, pose estimation, classification etc. This intent is similar to above intent but is specific to yolo models. It also includes selecting different inference option like cloud or local and visualize the output on images. If user asks about a use-case on images, for eg. face detection or car detection etc which you think can be fulfilled using any of the above models across any flavour (coco dataset), select this intent.
|
28 |
+
|
29 |
+
4. model_pipelining: This intent includes running two or multiple models one after the other in pipeline way to achieve something. For eg. for emotion classification usecase, we first run face detection model to extract faces and then run emotion classification model over it. So any query or usecase in the query is related to running two or models in pipeline mode, select this intent.
|
30 |
+
|
31 |
+
5. class_filtering: If a model has multiple classes but you only want to detect a particular class, there is a way to do it in degirum pysdk. So any question or query where user wants to detect only a particular set of classes out of all the classes model is trained on, select this intent. For eg. someone is using coco model but wants to detect only person and car.
|
32 |
+
|
33 |
+
6. overlay_effects: Degirum pysdk supports multiple overlay effects after we get results from the model like blurring the detected object, changing colour of the bounding box, changing size and position of the labels, changing font or showing probabilities, bounding box thickness etc. So any such query or use-case where some kind of overlay effects is required, use this intent.
|
34 |
+
|
35 |
+
7. running_inference_on_video: This is similar to intent #2 and #3 where we show running model inference on images but here we show inference on videos. Video can be saved video file, webcam stream or RTSP URL. So any query about running inference on live camera feed or saved video files, we should select this intent. For most real world use-cases, we will be running inference on videos but while prototyping user may need to test on images. So choose between this intent and intent 2 and 3 carefully.
|
36 |
+
|
37 |
+
8. person_re_identification_or_extracting_embeddings : There are some use-cases where user may need to extract embeddings from the image and store it or use it to calculate similarity etc. For eg. In person re-identification usecase, we extract face embeddings of a user and match it with the stored embeddings to identify if this is the same person. If user's query or use-case includes these kind of mechanism, select this intent.
|
38 |
+
|
39 |
+
9. zone_counting: Some use-cases includes selecting ROI for detection and then counting objects - for eg. counting number of people in a zone etc. For this, we have a specific function in DeGirum which allows you to draw a polygon for ROI and then count object (whichever class you specify) and returns that. If user query falls under this use-case, select this intent.
|
40 |
+
|
41 |
+
10. custom_video_source: Sometimes user may want to modify the incoming video source (may it be live stream like RTSP or saved video file) and apply some pre-processing to it like rotating, cropping, changing colour channels etc maybe to enhance detection. DeGirum PySDK allows user to modify incoming video stream before passing to model predictions. So any use-case or query related to modifying video source falls under this intent.
|
42 |
+
|
43 |
+
11. not_supported - Anything outside of the above intents will be something that degirum pysdk doesnt support. Dont try to forcibly put the query under any intent, if we dont support something its better to let user know this is not supported. This intent is specifically for avoiding hallucinations and throwing out made-up info to user even if we dont support that. So for any query about a feature which is not covered in above intents, select this intent.
|
44 |
+
|
45 |
+
Any question or query that I ask, analyse and think deeper. Try to identify the flow and think about all the intents applicable. Try to generalise the query and understand which intents are applicable. Dont focus on answering the query just select the intents from above list and return the intents that you feel are relevant. As I said, try to generalise the query and identify under which category this use-case falls into. If the feature is not listed above, return #11 intent which is not supported.
|
46 |
+
|
47 |
+
Upon asking question, just return the intent name, nothing else. Dont focus on answering the query. Just try to generalise the question or query and return the intents that match.
|
48 |
+
|
49 |
+
Example of chain of thought:
|
50 |
+
|
51 |
+
User: I want to blur the faces of females in the video stream
|
52 |
+
|
53 |
+
Think: It will require running a model that can detect faces. Then user will want to run a classification model to classify the gender of the detected faces.
|
54 |
+
And then user will want to apply some overlay effects to the video stream to blur the faces of females.
|
55 |
+
So it will require running two models one after the other. Hence it will require model pipelining. And also overlay effects to blur the faces.
|
56 |
+
|
57 |
+
Hence the intents applicable here are #4 model_pipelining and #6 overlay_effects.
|
58 |
+
Especially pay attention if user request requires using multiple models or model pipelines. Analyse the use-case of the user and think about the implementation to
|
59 |
+
identify the intents.
|
60 |
+
|
61 |
+
Response:
|
62 |
+
{{
|
63 |
+
"intents": ["model_pipelining", "overlay_effects"]
|
64 |
+
}}
|
65 |
+
Only return the list of intent names based on provided schema in JSON format. If there in only one intent, return it as a list.
|
66 |
+
|
67 |
+
'''
|
68 |
+
|
69 |
+
get_answer_system_prompt = '''
|
70 |
+
Degirum PySDK is a python based SDK that is built to support AI inference of computer vision models on multiple AI accelerators like Intel Openvino, Hailo, Memryx, N2X etc. The software stack provides unified way to run inference on any model across our model zoo or from a local model zoo and perform post processing over it. We are building a system to generate answer for our users questions on PySDK. Based on user's query, we have a system that identifies which examples or part of documentation can help generate answer for user query.
|
71 |
+
|
72 |
+
So I will provide you example codes with comments and documentations, your job is to analyse the examples and understand it conceptually. Based on that, identify what user is looking for and generate code accordingly. User's request may not be exact, but it can be derived from the example code that I pass to you as reference. Also, you can use commonly used opencv functions or other python frameworks to help satisfy user request, but dont try to make up any degirum PySDK functions which are not in the example references. Try to use PySDK code as much as possible. Try to generalize user's question to help better.
|
73 |
+
|
74 |
+
I will attach example references and ask users question, your job is to generate a response along with code as instructed above. '''
|
75 |
+
|
76 |
+
get_txt_files = {
|
77 |
+
"hello_world_of_pysdk": "/content/text_files/001_quick_start.txt",
|
78 |
+
"single_model_inference": "/content/text_files/object_detection_image.txt",
|
79 |
+
"running_yolo_models": "/content/text_files/002_yolov8.txt",
|
80 |
+
"model_pipelining": "/content/text_files/007_model_pipelining.txt",
|
81 |
+
"class_filtering": "/content/text_files/object_detection_class_filtering.txt",
|
82 |
+
"overlay_effects": "/content/text_files/013_overlay_effects.txt",
|
83 |
+
"running_inference_on_video": "/content/text_files/object_detection_video_stream.txt",
|
84 |
+
"person_re_identification_or_extracting_embeddings": "/content/text_files/014_person_reid.txt",
|
85 |
+
"zone_counting": "/content/text_files/009_zone_counting.txt",
|
86 |
+
"custom_video_source": "/content/text_files/016_custom_video_source.txt",
|
87 |
+
}
|
88 |
+
|
requirements.txt
ADDED
Binary file (2.27 kB). View file
|
|
text_files/001_quick_start.txt
ADDED
@@ -0,0 +1,156 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
### DeGirum PySDK Tutorial for Hailo8L/HAILO8
|
2 |
+
In this notebook, we illustrate the main features of PySDK and how it can be used to quickly develop edge AI applications using the `Hailo8L/HAILO8` accelerator. See [Setup Instructions for PySDK Notebooks](../README.md#setup-instructions-for-pysdk-notebooks).
|
3 |
+
|
4 |
+
DeGirum's PySDK provides simple APIs to run AI model inference. In general, there are three steps in running an AI model:
|
5 |
+
1. Loading model using `degirum.load_model` method
|
6 |
+
2. Running inference on an input using the `model.predict` method
|
7 |
+
3. Visualizing inference results using the `results.image_overlay` method
|
8 |
+
|
9 |
+
--------------------------------------------------------------------------------
|
10 |
+
|
11 |
+
# import degirum and degirum_tools
|
12 |
+
import degirum as dg, degirum_tools
|
13 |
+
|
14 |
+
inference_host_address = "@local"
|
15 |
+
zoo_url = 'degirum/hailo'
|
16 |
+
token=''
|
17 |
+
device_type='HAILORT/HAILO8L'
|
18 |
+
|
19 |
+
# set model name, and image source
|
20 |
+
model_name = "yolov8n_relu6_coco--640x640_quant_hailort_hailo8l_1"
|
21 |
+
image_source='../assets/ThreePersons.jpg'
|
22 |
+
|
23 |
+
# load AI model
|
24 |
+
model = dg.load_model(
|
25 |
+
model_name=model_name,
|
26 |
+
inference_host_address=inference_host_address,
|
27 |
+
zoo_url=zoo_url,
|
28 |
+
token=token,
|
29 |
+
device_type=device_type,
|
30 |
+
)
|
31 |
+
|
32 |
+
# perform AI model inference on given image source
|
33 |
+
print(f" Running inference using '{model_name}' on image source '{image_source}'")
|
34 |
+
inference_result = model(image_source)
|
35 |
+
|
36 |
+
# print('Inference Results \n', inference_result) # numeric results
|
37 |
+
print(inference_result)
|
38 |
+
print("Press 'x' or 'q' to stop.")
|
39 |
+
|
40 |
+
# show results of inference
|
41 |
+
with degirum_tools.Display("AI Camera") as output_display:
|
42 |
+
output_display.show_image(inference_result.image_overlay)
|
43 |
+
|
44 |
+
--------------------------------------------------------------------------------
|
45 |
+
|
46 |
+
#### Running Inference on Video Stream
|
47 |
+
- The `predict_stream` function in `degirum_tools` provides a powerful and efficient way to perform AI inference on video streams in real-time. It processes video frames sequentially and returns inference results frame by frame, enabling seamless integration with various video input sources.
|
48 |
+
- The code below shows how to use `predict_stream` on a video file.
|
49 |
+
|
50 |
+
--------------------------------------------------------------------------------
|
51 |
+
|
52 |
+
import degirum as dg, degirum_tools
|
53 |
+
|
54 |
+
inference_host_address = "@local"
|
55 |
+
zoo_url = 'degirum/hailo'
|
56 |
+
token=''
|
57 |
+
device_type='HAILORT/HAILO8L'
|
58 |
+
|
59 |
+
model_name = "yolov8n_relu6_coco--640x640_quant_hailort_hailo8l_1"
|
60 |
+
video_source = '../assets/Traffic.mp4'
|
61 |
+
|
62 |
+
# load AI model
|
63 |
+
model = dg.load_model(
|
64 |
+
model_name=model_name,
|
65 |
+
inference_host_address=inference_host_address,
|
66 |
+
zoo_url=zoo_url,
|
67 |
+
token=token,
|
68 |
+
device_type=device_type
|
69 |
+
)
|
70 |
+
|
71 |
+
with degirum_tools.Display("AI Camera") as output_display:
|
72 |
+
for inference_result in degirum_tools.predict_stream(model, video_source):
|
73 |
+
output_display.show(inference_result)
|
74 |
+
|
75 |
+
--------------------------------------------------------------------------------
|
76 |
+
|
77 |
+
### Listing Models
|
78 |
+
You can explore the models available using `degirum.list_models()` method.
|
79 |
+
|
80 |
+
--------------------------------------------------------------------------------
|
81 |
+
|
82 |
+
import degirum as dg, degirum_tools
|
83 |
+
|
84 |
+
inference_host_address = "@local"
|
85 |
+
zoo_url = 'degirum/hailo'
|
86 |
+
token=''
|
87 |
+
device_type=['HAILORT/HAILO8L']
|
88 |
+
|
89 |
+
model_list=dg.list_models(
|
90 |
+
inference_host_address=inference_host_address,
|
91 |
+
zoo_url=zoo_url,
|
92 |
+
token=token,
|
93 |
+
device_type=device_type
|
94 |
+
)
|
95 |
+
for index, model_name in enumerate(model_list):
|
96 |
+
print(index, model_name)
|
97 |
+
|
98 |
+
--------------------------------------------------------------------------------
|
99 |
+
|
100 |
+
### Switch to local model zoo
|
101 |
+
In this repo, we provide a `models` folder with a couple of example models. You can use the code block as a reference to run models from a local folder.
|
102 |
+
|
103 |
+
--------------------------------------------------------------------------------
|
104 |
+
|
105 |
+
import degirum as dg
|
106 |
+
|
107 |
+
inference_host_address = "@local"
|
108 |
+
zoo_url = '../models'
|
109 |
+
token=''
|
110 |
+
device_type='HAILORT/HAILO8L'
|
111 |
+
|
112 |
+
model_list = dg.list_models(
|
113 |
+
inference_host_address=inference_host_address,
|
114 |
+
zoo_url=zoo_url,
|
115 |
+
token=token,
|
116 |
+
device_type=device_type
|
117 |
+
)
|
118 |
+
|
119 |
+
for model_name in model_list.keys():
|
120 |
+
print(model_name)
|
121 |
+
|
122 |
+
--------------------------------------------------------------------------------
|
123 |
+
|
124 |
+
import degirum as dg, degirum_tools
|
125 |
+
|
126 |
+
inference_host_address = "@local"
|
127 |
+
zoo_url = '../models'
|
128 |
+
token=''
|
129 |
+
device_type='HAILORT/HAILO8L'
|
130 |
+
|
131 |
+
# set model name, inference host address, zoo url, token, and image source
|
132 |
+
model_name = "yolov8n_relu6_coco--640x640_quant_hailort_hailo8l_1"
|
133 |
+
image_source='../assets/ThreePersons.jpg'
|
134 |
+
|
135 |
+
# load AI model
|
136 |
+
model = dg.load_model(
|
137 |
+
model_name=model_name,
|
138 |
+
inference_host_address=inference_host_address,
|
139 |
+
zoo_url='../models',
|
140 |
+
token=token,
|
141 |
+
device_type=device_type
|
142 |
+
)
|
143 |
+
|
144 |
+
# perform AI model inference on given image source
|
145 |
+
print(f" Running inference using '{model_name}' on image source '{image_source}'")
|
146 |
+
inference_result = model(image_source)
|
147 |
+
|
148 |
+
# print('Inference Results \n', inference_result) # numeric results
|
149 |
+
print(inference_result)
|
150 |
+
print("Press 'x' or 'q' to stop.")
|
151 |
+
|
152 |
+
# show results of inference
|
153 |
+
with degirum_tools.Display("AI Camera") as output_display:
|
154 |
+
output_display.show_image(inference_result)
|
155 |
+
|
156 |
+
--------------------------------------------------------------------------------
|
text_files/002_yolov8.txt
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
### Running YOLO Models on Hailo
|
2 |
+
This notebook demonstrates running different flavors of YOLO models (e.g., image classification, object detection, pose estimation, and segmentation)
|
3 |
+
using a unified codebase with DeGirum PySDK. Key features include:
|
4 |
+
|
5 |
+
- Unified handling of YOLO model variants with minimal changes to the code.
|
6 |
+
- Flexible selection of inference host (cloud or local) and model zoo location.
|
7 |
+
- Seamless output visualization, regardless of the specific YOLO model used.
|
8 |
+
|
9 |
+
Simply uncomment a model of your choice, provide the necessary configurations, and run the code block
|
10 |
+
to perform inference and visualize the results.
|
11 |
+
|
12 |
+
--------------------------------------------------------------------------------
|
13 |
+
|
14 |
+
import degirum as dg, degirum_tools
|
15 |
+
|
16 |
+
inference_host_address = "@local"
|
17 |
+
zoo_url = 'degirum/hailo'
|
18 |
+
token=''
|
19 |
+
device_type=['HAILORT/HAILO8L']
|
20 |
+
|
21 |
+
# choose a model to run inference on by uncommenting one of the following lines
|
22 |
+
model_name = "yolov8n_relu6_coco--640x640_quant_hailort_hailo8l_1"
|
23 |
+
# model_name = "yolov8n_relu6_coco_pose--640x640_quant_hailort_hailo8l_1"
|
24 |
+
# model_name = "yolov8n_relu6_coco_seg--640x640_quant_hailort_hailo8l_1"
|
25 |
+
# model_name = "yolov8s_silu_imagenet--224x224_quant_hailort_hailo8l_1"
|
26 |
+
|
27 |
+
# choose image source
|
28 |
+
image_source = "../assets/ThreePersons.jpg"
|
29 |
+
|
30 |
+
# load AI model
|
31 |
+
model = dg.load_model(
|
32 |
+
model_name=model_name,
|
33 |
+
inference_host_address=inference_host_address,
|
34 |
+
zoo_url=zoo_url,
|
35 |
+
token=token,
|
36 |
+
device_type=device_type
|
37 |
+
)
|
38 |
+
|
39 |
+
# perform AI model inference on given image source
|
40 |
+
print(f" Running inference using '{model_name}' on image source '{image_source}'")
|
41 |
+
inference_result = model(image_source)
|
42 |
+
|
43 |
+
# print('Inference Results \n', inference_result) # numeric results
|
44 |
+
print(inference_result)
|
45 |
+
print("Press 'x' or 'q' to stop.")
|
46 |
+
|
47 |
+
# show results of inference
|
48 |
+
with degirum_tools.Display("AI Camera") as output_display:
|
49 |
+
output_display.show_image(inference_result.image_overlay)
|
50 |
+
|
51 |
+
--------------------------------------------------------------------------------
|
text_files/007_model_pipelining.txt
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
### Pipelining Two Models
|
2 |
+
This notebook is an example of how to use DeGirum PySDK to do AI inference of a video file using two AI models: face detection and gender classification. The face detection model is run on the image and the results are then processed by the gender classification model, one face at a time. Combined result is then displayed.
|
3 |
+
|
4 |
+
--------------------------------------------------------------------------------
|
5 |
+
|
6 |
+
import degirum as dg, degirum_tools
|
7 |
+
|
8 |
+
inference_host_address = "@local"
|
9 |
+
zoo_url = 'degirum/hailo'
|
10 |
+
token=''
|
11 |
+
device_type=['HAILORT/HAILO8L']
|
12 |
+
|
13 |
+
# specify model names
|
14 |
+
face_det_model_name = "yolov8n_relu6_face--640x640_quant_hailort_hailo8l_1"
|
15 |
+
gender_cls_model_name = "yolov8n_relu6_fairface_gender--256x256_quant_hailort_hailo8l_1"
|
16 |
+
|
17 |
+
# specify video source
|
18 |
+
video_source = "../assets/faces_and_gender.mp4"
|
19 |
+
|
20 |
+
# Load face detection and gender detection models
|
21 |
+
face_det_model = dg.load_model(
|
22 |
+
model_name=face_det_model_name,
|
23 |
+
inference_host_address=inference_host_address,
|
24 |
+
zoo_url=zoo_url,
|
25 |
+
token='',
|
26 |
+
device_type=device_type,
|
27 |
+
overlay_color=[(255,255,0),(0,255,0)]
|
28 |
+
)
|
29 |
+
|
30 |
+
gender_cls_model = dg.load_model(
|
31 |
+
model_name=gender_cls_model_name,
|
32 |
+
inference_host_address=inference_host_address,
|
33 |
+
zoo_url=zoo_url,
|
34 |
+
token='',
|
35 |
+
device_type=device_type,
|
36 |
+
)
|
37 |
+
|
38 |
+
# Create a compound cropping model with 20% crop extent
|
39 |
+
crop_model = degirum_tools.CroppingAndClassifyingCompoundModel(
|
40 |
+
face_det_model,
|
41 |
+
gender_cls_model,
|
42 |
+
30.0
|
43 |
+
)
|
44 |
+
|
45 |
+
# run AI inference on video stream
|
46 |
+
inference_results = degirum_tools.predict_stream(crop_model, video_source)
|
47 |
+
|
48 |
+
# display inference results
|
49 |
+
# Press 'x' or 'q' to stop
|
50 |
+
with degirum_tools.Display("Faces and Gender") as display:
|
51 |
+
for inference_result in inference_results:
|
52 |
+
display.show(inference_result.image_overlay)
|
53 |
+
|
54 |
+
--------------------------------------------------------------------------------
|
text_files/009_zone_counting.txt
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
### Interactive Zone-Based Object Counting with DeGirum PySDK
|
2 |
+
This script demonstrates how to perform object detection and counting within specific zones of interest using DeGirum PySDK. The key features include:
|
3 |
+
|
4 |
+
- Model-Based Object Detection: Utilizes the YOLOv8 model to detect objects such as cars, motorbikes, and trucks from a video stream.
|
5 |
+
- Zone-Based Counting: Counts objects within user-defined polygonal zones, defined as a list of coordinates.
|
6 |
+
- Interactive Zone Adjustment: The zones of interest can be interactively adjusted in real-time using the mouse within the display window, providing flexibility to adapt to changing scenarios.
|
7 |
+
- Customizable Output: Supports filtering specific object classes for counting and displays results per class.
|
8 |
+
- Stream Processing: Processes video streams, displaying both the detected objects and zone-based analytics in a dedicated window.
|
9 |
+
|
10 |
+
Simply define your zones, select the classes to track, and specify the video source to get started. This script is ideal for applications such as traffic monitoring, crowd analysis, and zone-specific surveillance.
|
11 |
+
|
12 |
+
--------------------------------------------------------------------------------
|
13 |
+
|
14 |
+
import degirum as dg, degirum_tools
|
15 |
+
|
16 |
+
|
17 |
+
inference_host_address = "@local"
|
18 |
+
zoo_url = "degirum/hailo"
|
19 |
+
token = ''
|
20 |
+
device_type = "HAILORT/HAILO8L"
|
21 |
+
|
22 |
+
# set the model name and video source
|
23 |
+
model_name = 'yolov8n_relu6_coco--640x640_quant_hailort_hailo8l_1'
|
24 |
+
video_source = '../assets/Traffic.mp4'
|
25 |
+
|
26 |
+
# define the zones of interest
|
27 |
+
polygon_zones = [
|
28 |
+
[[265, 260], [730, 260], [870, 450], [120, 450]],
|
29 |
+
[[400, 100], [610, 100], [690, 200], [320, 200]],
|
30 |
+
]
|
31 |
+
|
32 |
+
# define class list and display options
|
33 |
+
class_list = ["car", "motorbike", "truck"]
|
34 |
+
per_class_display = True
|
35 |
+
window_name="AI Camera"
|
36 |
+
|
37 |
+
# load model
|
38 |
+
model = dg.load_model(
|
39 |
+
model_name=model_name,
|
40 |
+
inference_host_address=inference_host_address,
|
41 |
+
zoo_url=zoo_url,
|
42 |
+
token=token,
|
43 |
+
overlay_color=[(255,0,0)],
|
44 |
+
output_class_set = set(class_list)
|
45 |
+
)
|
46 |
+
|
47 |
+
# create zone counter
|
48 |
+
zone_counter = degirum_tools.ZoneCounter(
|
49 |
+
polygon_zones,
|
50 |
+
class_list=class_list,
|
51 |
+
per_class_display=per_class_display,
|
52 |
+
triggering_position=degirum_tools.AnchorPoint.CENTER,
|
53 |
+
window_name=window_name, # attach display window for interactive zone adjustment
|
54 |
+
)
|
55 |
+
|
56 |
+
# attach zone counter to model
|
57 |
+
degirum_tools.attach_analyzers(model, [zone_counter])
|
58 |
+
|
59 |
+
# run inference and display results
|
60 |
+
with degirum_tools.Display(window_name) as display:
|
61 |
+
for inference_result in degirum_tools.predict_stream(model, video_source,):
|
62 |
+
display.show(inference_result)
|
63 |
+
|
64 |
+
--------------------------------------------------------------------------------
|
text_files/013_overlay_effects.txt
ADDED
@@ -0,0 +1,238 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Image overlay properties using DeGirum PySDK
|
2 |
+
In this notebook, we illustrate the different image overlay properties of DeGirum PySDK and how it can be used to display inference results with different types of formatting.
|
3 |
+
|
4 |
+
--------------------------------------------------------------------------------
|
5 |
+
|
6 |
+
import degirum as dg
|
7 |
+
import degirum_tools
|
8 |
+
|
9 |
+
# Define variables
|
10 |
+
model_name = 'yolov8n_coco--640x640_quant_hailort_hailo8l_1'
|
11 |
+
zoo_url = 'degirum/hailo'
|
12 |
+
inference_host_address = '@local'
|
13 |
+
device_type = 'HAILORT/HAILO8L'
|
14 |
+
token = ''
|
15 |
+
image_source = '../assets/dog_and_person.jpg'
|
16 |
+
|
17 |
+
--------------------------------------------------------------------------------
|
18 |
+
|
19 |
+
## Load Model
|
20 |
+
|
21 |
+
`model` object in DeGirum PySDK has a set of properties and methods that handles inference lifecycle tasks and also provides properties for overlay customization seperately. In this notebook, we will go through some of the examples reflecting overlay properties in PySDK.
|
22 |
+
|
23 |
+
Required arguments for `dg.load_model` are model name, host address, model zoo url, cloud token and device type.
|
24 |
+
|
25 |
+
--------------------------------------------------------------------------------
|
26 |
+
|
27 |
+
model = dg.load_model(
|
28 |
+
model_name=model_name,
|
29 |
+
inference_host_address=inference_host_address,
|
30 |
+
zoo_url=zoo_url,
|
31 |
+
token=token,
|
32 |
+
device_type=device_type
|
33 |
+
)
|
34 |
+
|
35 |
+
# perform AI model inference on given image source
|
36 |
+
print(f" Running inference using '{model_name}' on image source '{image_source}'")
|
37 |
+
inference_result = model(image_source)
|
38 |
+
|
39 |
+
print('\n Inference Results \n', inference_result) # numeric results
|
40 |
+
|
41 |
+
# show results of inference
|
42 |
+
with degirum_tools.Display("AI Camera") as output_display:
|
43 |
+
output_display.show_image(inference_result)
|
44 |
+
|
45 |
+
--------------------------------------------------------------------------------
|
46 |
+
|
47 |
+
### Show Class Labels
|
48 |
+
- Displays the class names (e.g., "person", "car") on top of detected objects in the overlay. Default value is `True`. Set to `False` to hide labels.
|
49 |
+
|
50 |
+
--------------------------------------------------------------------------------
|
51 |
+
|
52 |
+
# load AI model
|
53 |
+
model = dg.load_model(
|
54 |
+
model_name=model_name,
|
55 |
+
inference_host_address=inference_host_address,
|
56 |
+
zoo_url=zoo_url,
|
57 |
+
token=token,
|
58 |
+
device_type=device_type,
|
59 |
+
overlay_show_labels = False # set to True to show labels on the image
|
60 |
+
)
|
61 |
+
|
62 |
+
# perform AI model inference on given image source
|
63 |
+
print(f" Running inference using '{model_name}' on image source '{image_source}'")
|
64 |
+
inference_result = model(image_source)
|
65 |
+
|
66 |
+
print('\n Inference Results \n', inference_result) # numeric results
|
67 |
+
|
68 |
+
# show results of inference
|
69 |
+
with degirum_tools.Display("AI Camera") as output_display:
|
70 |
+
output_display.show_image(inference_result)
|
71 |
+
|
72 |
+
--------------------------------------------------------------------------------
|
73 |
+
|
74 |
+
### Show Class Probabilities
|
75 |
+
- Shows the prediction confidence score (e.g., 0.95) next to each detected label. Default value is `False`
|
76 |
+
|
77 |
+
--------------------------------------------------------------------------------
|
78 |
+
|
79 |
+
# load AI model
|
80 |
+
model = dg.load_model(
|
81 |
+
model_name=model_name,
|
82 |
+
inference_host_address=inference_host_address,
|
83 |
+
zoo_url=zoo_url,
|
84 |
+
token=token,
|
85 |
+
device_type=device_type,
|
86 |
+
overlay_show_probabilities = True
|
87 |
+
)
|
88 |
+
|
89 |
+
# perform AI model inference on given image source
|
90 |
+
print(f" Running inference using '{model_name}' on image source '{image_source}'")
|
91 |
+
inference_result = model(image_source)
|
92 |
+
|
93 |
+
print('\n Inference Results \n', inference_result) # numeric results
|
94 |
+
|
95 |
+
# show results of inference
|
96 |
+
with degirum_tools.Display("AI Camera") as output_display:
|
97 |
+
output_display.show_image(inference_result)
|
98 |
+
|
99 |
+
--------------------------------------------------------------------------------
|
100 |
+
|
101 |
+
### Set Font Scale
|
102 |
+
- Adjusts the size of the text displayed on the overlay (labels and probabilities). Its value is of type `float`, default being 1.0
|
103 |
+
|
104 |
+
--------------------------------------------------------------------------------
|
105 |
+
|
106 |
+
# load AI model
|
107 |
+
model = dg.load_model(
|
108 |
+
model_name=model_name,
|
109 |
+
inference_host_address=inference_host_address,
|
110 |
+
zoo_url=zoo_url,
|
111 |
+
token=token,
|
112 |
+
device_type=device_type,
|
113 |
+
overlay_font_scale = 3.2
|
114 |
+
)
|
115 |
+
|
116 |
+
# perform AI model inference on given image source
|
117 |
+
print(f" Running inference using '{model_name}' on image source '{image_source}'")
|
118 |
+
inference_result = model(image_source)
|
119 |
+
|
120 |
+
print('\n Inference Results \n', inference_result) # numeric results
|
121 |
+
|
122 |
+
# show results of inference
|
123 |
+
with degirum_tools.Display("AI Camera") as output_display:
|
124 |
+
output_display.show_image(inference_result)
|
125 |
+
|
126 |
+
--------------------------------------------------------------------------------
|
127 |
+
|
128 |
+
### Set Bounding Box Thickness
|
129 |
+
- Changes the thickness of the boxes drawn around detected objects. Its value is of type `float`, default being 3.0
|
130 |
+
|
131 |
+
--------------------------------------------------------------------------------
|
132 |
+
|
133 |
+
# load AI model
|
134 |
+
model = dg.load_model(
|
135 |
+
model_name=model_name,
|
136 |
+
inference_host_address=inference_host_address,
|
137 |
+
zoo_url=zoo_url,
|
138 |
+
token=token,
|
139 |
+
device_type=device_type,
|
140 |
+
overlay_line_width = 5
|
141 |
+
)
|
142 |
+
|
143 |
+
# perform AI model inference on given image source
|
144 |
+
print(f" Running inference using '{model_name}' on image source '{image_source}'")
|
145 |
+
inference_result = model(image_source)
|
146 |
+
|
147 |
+
print('\n Inference Results \n', inference_result) # numeric results
|
148 |
+
|
149 |
+
|
150 |
+
# show results of inference
|
151 |
+
with degirum_tools.Display("AI Camera") as output_display:
|
152 |
+
output_display.show_image(inference_result)
|
153 |
+
|
154 |
+
--------------------------------------------------------------------------------
|
155 |
+
|
156 |
+
### Set Overlay Box Color
|
157 |
+
- Defines the color of bounding boxes using RGB format. Default value is Black RGB(0,0,0).
|
158 |
+
- overlay_color can also be `(list[tuple[int,int,int]])`: Palette of RGB colors for drawing results; each tuple is an (R, G, B) value.
|
159 |
+
Color for a detection is chosen as `overlay_color[class_id % len(overlay_color)]` (class-ID modulo palette length).
|
160 |
+
|
161 |
+
--------------------------------------------------------------------------------
|
162 |
+
|
163 |
+
# load AI model
|
164 |
+
model = dg.load_model(
|
165 |
+
model_name=model_name,
|
166 |
+
inference_host_address=inference_host_address,
|
167 |
+
zoo_url=zoo_url,
|
168 |
+
token=token,
|
169 |
+
device_type=device_type,
|
170 |
+
overlay_color = [(255, 0, 0), (0, 255, 0), (0,0,255)] # red and green and blue colors
|
171 |
+
)
|
172 |
+
|
173 |
+
# perform AI model inference on given image source
|
174 |
+
print(f" Running inference using '{model_name}' on image source '{image_source}'")
|
175 |
+
inference_result = model(image_source)
|
176 |
+
|
177 |
+
print('\n Inference Results \n', inference_result) # numeric results
|
178 |
+
|
179 |
+
|
180 |
+
# show results of inference
|
181 |
+
with degirum_tools.Display("AI Camera") as output_display:
|
182 |
+
output_display.show_image(inference_result)
|
183 |
+
|
184 |
+
--------------------------------------------------------------------------------
|
185 |
+
|
186 |
+
### Set Overlay Transparency
|
187 |
+
- Controls the transparency of the overlay bounding boxes that appears over the image. This value is a `float` ranging from 0 to 1, where 1 represents full opacity and 0 indicates near invisibility. Default value is 1.0
|
188 |
+
|
189 |
+
--------------------------------------------------------------------------------
|
190 |
+
|
191 |
+
# load AI model
|
192 |
+
model = dg.load_model(
|
193 |
+
model_name=model_name,
|
194 |
+
inference_host_address=inference_host_address,
|
195 |
+
zoo_url=zoo_url,
|
196 |
+
token=token,
|
197 |
+
device_type=device_type,
|
198 |
+
overlay_alpha = 0.5 # 50% transparent overlay
|
199 |
+
)
|
200 |
+
|
201 |
+
# perform AI model inference on given image source
|
202 |
+
print(f" Running inference using '{model_name}' on image source '{image_source}'")
|
203 |
+
inference_result = model(image_source)
|
204 |
+
|
205 |
+
print('\n Inference Results \n', inference_result) # numeric results
|
206 |
+
|
207 |
+
# show results of inference
|
208 |
+
with degirum_tools.Display("AI Camera") as output_display:
|
209 |
+
output_display.show_image(inference_result)
|
210 |
+
|
211 |
+
--------------------------------------------------------------------------------
|
212 |
+
|
213 |
+
### Blur Detected Objects
|
214 |
+
- Applies a blur effect to each detected object in the overlay. You can specify particular object classes to blur, or use `"all"` to blur every detected object. The default value is `None`.
|
215 |
+
|
216 |
+
--------------------------------------------------------------------------------
|
217 |
+
|
218 |
+
# load AI model
|
219 |
+
model = dg.load_model(
|
220 |
+
model_name=model_name,
|
221 |
+
inference_host_address=inference_host_address,
|
222 |
+
zoo_url=zoo_url,
|
223 |
+
token=token,
|
224 |
+
device_type=device_type,
|
225 |
+
overlay_blur = "dog"
|
226 |
+
)
|
227 |
+
|
228 |
+
# perform AI model inference on given image source
|
229 |
+
print(f" Running inference using '{model_name}' on image source '{image_source}'")
|
230 |
+
inference_result = model(image_source)
|
231 |
+
|
232 |
+
print('\n Inference Results \n', inference_result) # numeric results
|
233 |
+
|
234 |
+
# show results of inference
|
235 |
+
with degirum_tools.Display("AI Camera") as output_display:
|
236 |
+
output_display.show_image(inference_result)
|
237 |
+
|
238 |
+
--------------------------------------------------------------------------------
|
text_files/014_person_reid.txt
ADDED
@@ -0,0 +1,182 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Person Re-identification using PySDK
|
2 |
+
This notebook demonstrates Person Re-Identification (Re-ID) using PySDK. Re-ID focuses on recognizing and matching people across different camera views based on their unique appearance, like clothing and body shape.
|
3 |
+
|
4 |
+
The basic pipeline works like this:
|
5 |
+
1. Detect people in the image using a person detection model.
|
6 |
+
2. Crop each detected person using the bounding box coordinates.
|
7 |
+
3. Apply the Person Re-ID model to the cropped images to extract the embeddings which can further be used to identify and match individuals across different images or camera views.
|
8 |
+
|
9 |
+
--------------------------------------------------------------------------------
|
10 |
+
|
11 |
+
import degirum as dg, degirum_tools
|
12 |
+
|
13 |
+
inference_host_address = "@local"
|
14 |
+
zoo_url = "degirum/hailo"
|
15 |
+
token = ''
|
16 |
+
device_type = "HAILORT/HAILO8L"
|
17 |
+
|
18 |
+
# Person detection model name
|
19 |
+
person_det_model_name = "yolov8n_relu6_person--640x640_quant_hailort_hailo8l_1"
|
20 |
+
|
21 |
+
# load AI model
|
22 |
+
person_det_model = dg.load_model(
|
23 |
+
model_name=person_det_model_name,
|
24 |
+
inference_host_address=inference_host_address,
|
25 |
+
zoo_url=zoo_url,
|
26 |
+
token=token,
|
27 |
+
device_type=device_type
|
28 |
+
)
|
29 |
+
|
30 |
+
# Choose the Person reid model name
|
31 |
+
person_reid_model_name = "osnet_x1_0_person_reid--256x128_quant_hailort_hailo8l_1"
|
32 |
+
# person_reid_model_name = "repvgg_a0_person_reid--256x128_quant_hailort_hailo8l_1"
|
33 |
+
|
34 |
+
# load AI model
|
35 |
+
person_reid_model = dg.load_model(
|
36 |
+
model_name=person_reid_model_name,
|
37 |
+
inference_host_address=inference_host_address,
|
38 |
+
zoo_url=zoo_url,
|
39 |
+
token=token,
|
40 |
+
device_type=device_type
|
41 |
+
)
|
42 |
+
|
43 |
+
--------------------------------------------------------------------------------
|
44 |
+
|
45 |
+
#### Let's walk through a practical example of person re-identification.<br>
|
46 |
+
|
47 |
+
## Example Scenario
|
48 |
+
We use two sets of images:
|
49 |
+
- **Set A:** Same person, different views.
|
50 |
+
- **Set B:** Another individual, different views.
|
51 |
+
|
52 |
+
The goal is to verify whether the ReID model can:
|
53 |
+
|
54 |
+
- Correctly match all images in Set A or B as the same person.
|
55 |
+
- Correctly distinguish Set B as a different person from Set A.
|
56 |
+
|
57 |
+
This simulates a real-world scenario where the model must recognize individuals across different camera views and lighting conditions.
|
58 |
+
|
59 |
+
--------------------------------------------------------------------------------
|
60 |
+
|
61 |
+
# Utility function for displaying images
|
62 |
+
|
63 |
+
import matplotlib.pyplot as plt
|
64 |
+
|
65 |
+
def display_images(images, titles="Images", figsize=(15, 5)):
|
66 |
+
"""
|
67 |
+
Display a list of images in a single row using Matplotlib.
|
68 |
+
|
69 |
+
Parameters:
|
70 |
+
- images (list): List of images (NumPy arrays) to display.
|
71 |
+
- titles (str or list): Either a single string for overall title, or list of titles for each image.
|
72 |
+
- figsize (tuple): Size of the figure.
|
73 |
+
"""
|
74 |
+
num_images = len(images)
|
75 |
+
fig, axes = plt.subplots(1, num_images, figsize=figsize)
|
76 |
+
if num_images == 1:
|
77 |
+
axes = [axes] # Make iterable for single image
|
78 |
+
|
79 |
+
for i, (ax, image) in enumerate(zip(axes, images)):
|
80 |
+
image_rgb = image[:, :, ::-1] # Convert BGR to RGB
|
81 |
+
ax.imshow(image_rgb)
|
82 |
+
ax.axis('off')
|
83 |
+
if isinstance(titles, list) and i < len(titles):
|
84 |
+
ax.set_title(titles[i], fontsize=12)
|
85 |
+
|
86 |
+
if isinstance(titles, str):
|
87 |
+
fig.suptitle(titles, fontsize=16)
|
88 |
+
|
89 |
+
plt.tight_layout()
|
90 |
+
plt.show()
|
91 |
+
|
92 |
+
--------------------------------------------------------------------------------
|
93 |
+
|
94 |
+
#Image sources
|
95 |
+
image_source_1 = "../assets/person_1_multi_view.png"
|
96 |
+
image_source_2 = "../assets/person_2_multi_view.png"
|
97 |
+
|
98 |
+
# Detections
|
99 |
+
detections_1 = person_det_model(image_source_1)
|
100 |
+
detections_2 = person_det_model(image_source_2)
|
101 |
+
|
102 |
+
display_images([detections_1.image_overlay, detections_2.image_overlay], titles=["Person_1 Detections", "Person_2 Detections"], figsize=(10, 5))
|
103 |
+
|
104 |
+
--------------------------------------------------------------------------------
|
105 |
+
|
106 |
+
# Cropping
|
107 |
+
|
108 |
+
# Crops from the image_source_1
|
109 |
+
x1, y1, x2, y2 = map(int, detections_1.results[0]["bbox"]) # Convert bbox coordinates to integers
|
110 |
+
person1_crop1 = detections_1.image[y1:y2, x1:x2] # Crop the person from the image
|
111 |
+
|
112 |
+
x1, y1, x2, y2 = map(int, detections_1.results[1]["bbox"]) # Convert bbox coordinates to integers
|
113 |
+
person1_crop2 = detections_1.image[y1:y2, x1:x2] # Crop the person from the image
|
114 |
+
|
115 |
+
# Crops from the image_source_2
|
116 |
+
x1, y1, x2, y2 = map(int, detections_2.results[0]["bbox"]) # Convert bbox coordinates to integers
|
117 |
+
person2_crop1 = detections_2.image[y1:y2, x1:x2] # Crop the person from the image
|
118 |
+
|
119 |
+
x1, y1, x2, y2 = map(int, detections_2.results[1]["bbox"]) # Convert bbox coordinates to integers
|
120 |
+
person2_crop2 = detections_2.image[y1:y2, x1:x2] # Crop the person from the image
|
121 |
+
|
122 |
+
# Display person crops
|
123 |
+
display_images([person1_crop1, person1_crop2, person2_crop1, person2_crop2], titles=["Person1 Crop1","Person1 Crop2","Person2 Crop1", "Person2 Crop2"], figsize=(10, 5))
|
124 |
+
|
125 |
+
--------------------------------------------------------------------------------
|
126 |
+
|
127 |
+
### Extracting embedding using a Re-Identification (ReID) model for each person crop
|
128 |
+
|
129 |
+
--------------------------------------------------------------------------------
|
130 |
+
|
131 |
+
import numpy as np
|
132 |
+
#Extract the embeddings for the image_source_1 that has two crops
|
133 |
+
embedding_person1_crop1 = np.asarray(person_reid_model(person1_crop1).results[0]["data"]).reshape(1, -1) # shape (1,512)
|
134 |
+
embedding_person1_crop2 = np.asarray(person_reid_model(person1_crop2).results[0]["data"]).reshape(1, -1) # shape (1,512)
|
135 |
+
|
136 |
+
#Extract the embeddings for the image_source_2 that detected two crops
|
137 |
+
embedding_person2_crop1 = np.asarray(person_reid_model(person2_crop1).results[0]["data"]).reshape(1, -1) # shape (1,512)
|
138 |
+
embedding_person2_crop2 = np.asarray(person_reid_model(person2_crop2).results[0]["data"]).reshape(1, -1) # shape (1,512)
|
139 |
+
|
140 |
+
--------------------------------------------------------------------------------
|
141 |
+
|
142 |
+
### Calculating cosine similarity between the embeddings
|
143 |
+
|
144 |
+
--------------------------------------------------------------------------------
|
145 |
+
|
146 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
147 |
+
|
148 |
+
# Compute cosine similarity between crops of the same person on different camera views
|
149 |
+
similarity_person1 = cosine_similarity(embedding_person1_crop1, embedding_person1_crop2)
|
150 |
+
print("Cosine similarity between Person1 crops (two images of the person1 with different camera views):",similarity_person1)
|
151 |
+
|
152 |
+
similarity_person2 = cosine_similarity(embedding_person2_crop1, embedding_person2_crop2)
|
153 |
+
print("Cosine similarity between Person2 crops (two images of the person2 with different camera views):", similarity_person2, "\n")
|
154 |
+
|
155 |
+
|
156 |
+
# Compute cosine similarity between crops of person1 and person2
|
157 |
+
similarity_p1c1_p2c1 = cosine_similarity(embedding_person1_crop1, embedding_person2_crop1)
|
158 |
+
print("Person 1 Crop 1 vs Person 2 Crop 1:", similarity_p1c1_p2c1)
|
159 |
+
|
160 |
+
similarity_p1c1_p2c2 = cosine_similarity(embedding_person1_crop1, embedding_person2_crop2)
|
161 |
+
print("Person 1 Crop 1 vs Person 2 Crop 2:", similarity_p1c1_p2c2)
|
162 |
+
|
163 |
+
similarity_p1c2_p2c1 = cosine_similarity(embedding_person1_crop2, embedding_person2_crop1)
|
164 |
+
print("Person 1 Crop 2 vs Person 2 Crop 1:", similarity_p1c2_p2c1)
|
165 |
+
|
166 |
+
similarity_p1c2_p2c2 = cosine_similarity(embedding_person1_crop2, embedding_person2_crop2)
|
167 |
+
print("Person 1 Crop 2 vs Person 2 Crop 2:", similarity_p1c2_p2c2)
|
168 |
+
|
169 |
+
--------------------------------------------------------------------------------
|
170 |
+
|
171 |
+
### Interpreting Cosine Similarity Results
|
172 |
+
These results indicate that the Re-ID Osnet model effectively distinguishes between the same and different individuals:
|
173 |
+
|
174 |
+
- **Intra-person similarities** (Person 1 & Person 2 across views) are consistently **high (~0.87–0.88)**.
|
175 |
+
- **Inter-person similarities** (between different individuals) are **significantly lower (~0.44–0.55)**.
|
176 |
+
|
177 |
+
In person re-identification, it's common to apply a **similarity threshold** (typically in the range of ~0.7–0.8) to determine whether two embeddings represent the same individual. <br>
|
178 |
+
This threshold serves as a decision boundary: if the cosine similarity between two feature vectors (embeddings) exceeds the threshold, they are considered to belong to the same person; otherwise, they are treated as different individuals.
|
179 |
+
|
180 |
+
The ideal threshold may vary — it's typically fine-tuned on a validation set to achieve the best trade-off between false positives and false negatives, and it often depends on the specific model architecture and characteristics of the dataset. The choice involves balancing False positives and False negatives.
|
181 |
+
|
182 |
+
--------------------------------------------------------------------------------
|
text_files/016_custom_video_source.txt
ADDED
@@ -0,0 +1,256 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
## Feed custom video source to `predict_batch` to DeGirum PySDK
|
2 |
+
|
3 |
+
This guide is for developers who want to learn how to configure a custom video generator and feed it to DeGirum PySDK to use it for model predictions. In this guide, we will walk you through the steps on creating your own custom video generator and pass it to model object's `predict_batch` function to perform prediction over it.
|
4 |
+
|
5 |
+
|
6 |
+
--------------------------------------------------------------------------------
|
7 |
+
|
8 |
+
### Simple predictions on video stream
|
9 |
+
|
10 |
+
To begin with, lets take a look at how to run predictions on a simple video stream as is. For this, we will start with a simple frame generator function as below:
|
11 |
+
|
12 |
+
--------------------------------------------------------------------------------
|
13 |
+
|
14 |
+
# Define generator function to produce video frames
|
15 |
+
def frame_source(stream):
|
16 |
+
while True:
|
17 |
+
ret, frame = stream.read()
|
18 |
+
if not ret:
|
19 |
+
break # end of file
|
20 |
+
yield frame
|
21 |
+
|
22 |
+
--------------------------------------------------------------------------------
|
23 |
+
|
24 |
+
We use this function to pass it to our `predict_batch` function as below:
|
25 |
+
|
26 |
+
--------------------------------------------------------------------------------
|
27 |
+
|
28 |
+
for result in model.predict_batch(frame_source(stream)):
|
29 |
+
# Print raw results for each frame
|
30 |
+
print(result)
|
31 |
+
|
32 |
+
|
33 |
+
--------------------------------------------------------------------------------
|
34 |
+
|
35 |
+
Putting it all together, lets run inference of a video source using simple video generator and `predict_batch` function:
|
36 |
+
|
37 |
+
--------------------------------------------------------------------------------
|
38 |
+
|
39 |
+
import degirum as dg
|
40 |
+
import cv2
|
41 |
+
|
42 |
+
# Declaring variables
|
43 |
+
# Set your model, inference host address, model zoo, and token in these variables.
|
44 |
+
your_model_name = "model-name"
|
45 |
+
your_host_address = "@local" # Can be dg.CLOUD, host:port, or dg.LOCAL
|
46 |
+
your_model_zoo = "degirum/hailo"
|
47 |
+
your_token = "<token>"
|
48 |
+
|
49 |
+
# Specify the video you will run inference on
|
50 |
+
your_video = "path/video.mp4"
|
51 |
+
|
52 |
+
# Loading a model
|
53 |
+
model = dg.load_model(
|
54 |
+
model_name = your_model_name,
|
55 |
+
inference_host_address = your_host_address,
|
56 |
+
zoo_url = your_model_zoo,
|
57 |
+
token = your_token
|
58 |
+
# optional parameters, such as overlay_show_probabilities = True
|
59 |
+
)
|
60 |
+
|
61 |
+
# Open your video file
|
62 |
+
stream = cv2.VideoCapture(your_video)
|
63 |
+
|
64 |
+
# Define generator function to produce video frames
|
65 |
+
def frame_source(stream):
|
66 |
+
while True:
|
67 |
+
ret, frame = stream.read()
|
68 |
+
if not ret:
|
69 |
+
break # end of file
|
70 |
+
yield frame
|
71 |
+
|
72 |
+
# Run predict_batch on stream of frames from video file
|
73 |
+
for result in model.predict_batch(frame_source(stream)):
|
74 |
+
# Print raw results for each frame
|
75 |
+
print(result)
|
76 |
+
|
77 |
+
# Release stream
|
78 |
+
stream.release()
|
79 |
+
|
80 |
+
--------------------------------------------------------------------------------
|
81 |
+
|
82 |
+
#### Modify video generator to use webcam or Rpi stream
|
83 |
+
|
84 |
+
In order to use webcam or an Rpi stream as video source, we will just modify our video source_path as below:
|
85 |
+
|
86 |
+
--------------------------------------------------------------------------------
|
87 |
+
|
88 |
+
##### Webcam input
|
89 |
+
|
90 |
+
--------------------------------------------------------------------------------
|
91 |
+
|
92 |
+
import degirum as dg
|
93 |
+
import cv2
|
94 |
+
|
95 |
+
# Declaring variables
|
96 |
+
# Set your model, inference host address, model zoo, and token in these variables.
|
97 |
+
your_model_name = "model-name"
|
98 |
+
your_host_address = "@local" # Can be dg.CLOUD, host:port, or dg.LOCAL
|
99 |
+
your_model_zoo = "degirum/hailo"
|
100 |
+
your_token = "<token>"
|
101 |
+
|
102 |
+
|
103 |
+
# Loading a model
|
104 |
+
model = dg.load_model(
|
105 |
+
model_name = your_model_name,
|
106 |
+
inference_host_address = your_host_address,
|
107 |
+
zoo_url = your_model_zoo,
|
108 |
+
token = your_token
|
109 |
+
# optional parameters, such as overlay_show_probabilities = True
|
110 |
+
)
|
111 |
+
|
112 |
+
# Open your video file
|
113 |
+
stream = cv2.VideoCapture(0) # Webcam source : 0
|
114 |
+
|
115 |
+
# Define generator function to produce video frames
|
116 |
+
def frame_source(stream):
|
117 |
+
while True:
|
118 |
+
ret, frame = stream.read()
|
119 |
+
if not ret:
|
120 |
+
break # end of file
|
121 |
+
yield frame
|
122 |
+
|
123 |
+
# Run predict_batch on stream of frames from video file
|
124 |
+
for result in model.predict_batch(frame_source(stream)):
|
125 |
+
# Print raw results for each frame
|
126 |
+
print(result)
|
127 |
+
|
128 |
+
# Release stream
|
129 |
+
stream.release()
|
130 |
+
|
131 |
+
--------------------------------------------------------------------------------
|
132 |
+
|
133 |
+
#### Rpi Camera using PiCamera Module
|
134 |
+
Similar to above example where we used OpenCV to capture frames from webcam, we can use `Picamera2` module to fetch frames from an Rpi Camera inside our frame generator function and pass that to `predict_batch`
|
135 |
+
|
136 |
+
--------------------------------------------------------------------------------
|
137 |
+
|
138 |
+
import cv2
|
139 |
+
import degirum as dg
|
140 |
+
import numpy as np
|
141 |
+
from picamera2 import Picamera2
|
142 |
+
|
143 |
+
your_model_name = "model-name"
|
144 |
+
your_host_address = "@local" # Can be dg.CLOUD, host:port, or dg.LOCAL
|
145 |
+
your_model_zoo = "degirum/hailo"
|
146 |
+
your_token = "<token>"
|
147 |
+
|
148 |
+
# Load the model
|
149 |
+
model = dg.load_model(
|
150 |
+
model_name = your_model_name,
|
151 |
+
inference_host_address = your_host_address,
|
152 |
+
zoo_url = your_model_zoo,
|
153 |
+
token = your_token
|
154 |
+
# optional parameters, such as overlay_show_probabilities = True
|
155 |
+
)
|
156 |
+
|
157 |
+
# Define frame generator using Picamera2
|
158 |
+
def picamera2_frame_generator():
|
159 |
+
picam2 = Picamera2()
|
160 |
+
picam2.configure(picam2.preview_configuration(main={"format": 'BGR888'}))
|
161 |
+
picam2.start()
|
162 |
+
try:
|
163 |
+
while True:
|
164 |
+
frame = picam2.capture_array()
|
165 |
+
yield frame
|
166 |
+
finally:
|
167 |
+
picam2.stop()
|
168 |
+
|
169 |
+
# Run inference and display
|
170 |
+
for result in model.predict_batch(picamera2_frame_generator(rotate=True)):
|
171 |
+
cv2.imshow("AI Inference PiCamera2", result.image_overlay)
|
172 |
+
if cv2.waitKey(1) & 0xFF == ord("q"):
|
173 |
+
break
|
174 |
+
|
175 |
+
cv2.destroyAllWindows()
|
176 |
+
|
177 |
+
|
178 |
+
--------------------------------------------------------------------------------
|
179 |
+
|
180 |
+
#### Transform your video source before predictions
|
181 |
+
|
182 |
+
Lets a take a look at how can you perform pre-processing or transformation on your input video source like rotation, resize, crop etc and perform model prediction over it. In this example, we will take camera input and rotate it 90' clockwise before passing it to `predict_batch` function.
|
183 |
+
|
184 |
+
--------------------------------------------------------------------------------
|
185 |
+
|
186 |
+
For this, we will modify our video generator as follows:
|
187 |
+
|
188 |
+
--------------------------------------------------------------------------------
|
189 |
+
|
190 |
+
def rotated_frame_generator(video_source):
|
191 |
+
stream = cv2.VideoCapture(video_source)
|
192 |
+
try:
|
193 |
+
while True:
|
194 |
+
ret, frame = stream.read()
|
195 |
+
if not ret:
|
196 |
+
break
|
197 |
+
rotated_frame = cv2.rotate(frame, cv2.ROTATE_90_CLOCKWISE) # Rotate frame 90 degrees clockwise
|
198 |
+
yield rotated_frame
|
199 |
+
finally:
|
200 |
+
stream.release()
|
201 |
+
|
202 |
+
--------------------------------------------------------------------------------
|
203 |
+
|
204 |
+
Putting it all together, it looks like this:
|
205 |
+
|
206 |
+
--------------------------------------------------------------------------------
|
207 |
+
|
208 |
+
import degirum as dg
|
209 |
+
import degirum_tools.video_support as vs
|
210 |
+
import cv2
|
211 |
+
import degirum_tools
|
212 |
+
|
213 |
+
hw_location = "@local"
|
214 |
+
model_zoo_url = "model_zoo_url"
|
215 |
+
model_name = "your_model_name"
|
216 |
+
video_source_path = 0 # Webcam source : 0
|
217 |
+
degirum_cloud_token = "<token>" # Optional, only needed for cloud inference
|
218 |
+
|
219 |
+
# Load the model
|
220 |
+
model = dg.load_model(
|
221 |
+
model_name=model_name,
|
222 |
+
inference_host_address=hw_location,
|
223 |
+
zoo_url=model_zoo_url,
|
224 |
+
token=degirum_cloud_token,
|
225 |
+
overlay_color=(0, 255, 0)
|
226 |
+
)
|
227 |
+
|
228 |
+
# Define rotated frame generator
|
229 |
+
def rotated_frame_generator(video_source):
|
230 |
+
stream = cv2.VideoCapture(video_source)
|
231 |
+
try:
|
232 |
+
while True:
|
233 |
+
ret, frame = stream.read()
|
234 |
+
if not ret:
|
235 |
+
break
|
236 |
+
rotated_frame = cv2.rotate(frame, cv2.ROTATE_90_CLOCKWISE) # Rotate frame 90 degrees clockwise
|
237 |
+
yield rotated_frame
|
238 |
+
finally:
|
239 |
+
stream.release()
|
240 |
+
|
241 |
+
# Run inference and display
|
242 |
+
for result in model.predict_batch(rotated_frame_generator(video_source_path)):
|
243 |
+
cv2.imshow("AI Inference", result.image_overlay)
|
244 |
+
if cv2.waitKey(1) & 0xFF == ord("q"):
|
245 |
+
break
|
246 |
+
|
247 |
+
cv2.destroyAllWindows()
|
248 |
+
|
249 |
+
|
250 |
+
--------------------------------------------------------------------------------
|
251 |
+
|
252 |
+
#### Conclusion
|
253 |
+
|
254 |
+
In this guide, we have walked through the steps of setting up a custom video frame generator and pass it to model predictions using DeGirum PySDK. These steps allows you to modify and transform your video source feed and perform different operations such as rotation, crop etc and use the transformed video frames for model prediction. Such a process is useful especially when you want to modify your original source input for better accuracy on model predictions.
|
255 |
+
|
256 |
+
--------------------------------------------------------------------------------
|
text_files/object_detection_class_filtering.txt
ADDED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+

|
2 |
+
## Object detection with class filtering on a video stream
|
3 |
+
This notebook is a simple example of how to use DeGirum PySDK to do object detection AI inference
|
4 |
+
on an image file filtering only desired set of classes.
|
5 |
+
|
6 |
+
This script works with the following inference options:
|
7 |
+
|
8 |
+
1. Run inference on DeGirum Cloud Platform;
|
9 |
+
2. Run inference on DeGirum AI Server deployed on a localhost or on some computer in your LAN or VPN;
|
10 |
+
3. Run inference on DeGirum ORCA accelerator directly installed on your computer.
|
11 |
+
|
12 |
+
To try different options, you need to specify the appropriate `hw_location` option.
|
13 |
+
|
14 |
+
When running this notebook locally, you need to specify your cloud API access token in the [env.ini](../../env.ini) file, located in the same directory as this notebook.
|
15 |
+
|
16 |
+
When running this notebook in Google Colab, the cloud API access token should be stored in a user secret named `DEGIRUM_CLOUD_TOKEN`.
|
17 |
+
|
18 |
+
|
19 |
+
--------------------------------------------------------------------------------
|
20 |
+
|
21 |
+
# make sure degirum-tools package is installed
|
22 |
+
!pip show degirum-tools || pip install degirum-tools
|
23 |
+
|
24 |
+
--------------------------------------------------------------------------------
|
25 |
+
|
26 |
+
#### Specify where you want to run your inferences, model zoo url, model name and video source
|
27 |
+
|
28 |
+
--------------------------------------------------------------------------------
|
29 |
+
|
30 |
+
# hw_location: where you want to run inference
|
31 |
+
# "@cloud" to use DeGirum cloud
|
32 |
+
# "@local" to run on local machine
|
33 |
+
# IP address for AI server inference
|
34 |
+
# model_zoo_url: url/path for model zoo
|
35 |
+
# cloud_zoo_url: valid for @cloud, @local, and ai server inference options
|
36 |
+
# '': ai server serving models from local folder
|
37 |
+
# path to json file: single model zoo in case of @local inference
|
38 |
+
# model_name: name of the model for running AI inference
|
39 |
+
# image: image source for inference
|
40 |
+
# URL of image file
|
41 |
+
# path to image file (jpg, png, etc)
|
42 |
+
# classes: set of class labels to accept
|
43 |
+
hw_location = "@cloud"
|
44 |
+
model_zoo_url = "degirum/public"
|
45 |
+
model_name = "yolo_v5s_coco--512x512_quant_n2x_orca1_1"
|
46 |
+
image = "https://raw.githubusercontent.com/DeGirum/PySDKExamples/main/images/bikes.jpg"
|
47 |
+
classes = {"bicycle"}
|
48 |
+
|
49 |
+
--------------------------------------------------------------------------------
|
50 |
+
|
51 |
+
#### The rest of the cells below should run without any modifications
|
52 |
+
|
53 |
+
--------------------------------------------------------------------------------
|
54 |
+
|
55 |
+
import degirum as dg, degirum_tools
|
56 |
+
|
57 |
+
# connect to AI inference engine and load object detection AI model
|
58 |
+
model = dg.load_model(
|
59 |
+
model_name=model_name,
|
60 |
+
inference_host_address=hw_location,
|
61 |
+
zoo_url=model_zoo_url,
|
62 |
+
token=degirum_tools.get_token(),
|
63 |
+
)
|
64 |
+
|
65 |
+
# AI prediction: show only desired classes
|
66 |
+
with degirum_tools.Display("All classes (press 'q' to exit)") as display:
|
67 |
+
inference_result = model(image)
|
68 |
+
display.show_image(inference_result)
|
69 |
+
|
70 |
+
--------------------------------------------------------------------------------
|
71 |
+
|
72 |
+
# connect to AI inference engine and load object detection AI model
|
73 |
+
# setting `output_class_set` to desired classes
|
74 |
+
model = dg.load_model(
|
75 |
+
model_name=model_name,
|
76 |
+
inference_host_address=hw_location,
|
77 |
+
zoo_url=model_zoo_url,
|
78 |
+
token=degirum_tools.get_token(),
|
79 |
+
output_class_set=classes
|
80 |
+
)
|
81 |
+
|
82 |
+
# AI prediction: show only desired classes
|
83 |
+
with degirum_tools.Display("Only bikes (press 'q' to exit)") as display:
|
84 |
+
inference_result = model(image)
|
85 |
+
display.show_image(inference_result)
|
86 |
+
|
87 |
+
--------------------------------------------------------------------------------
|
text_files/object_detection_image.txt
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+

|
2 |
+
## Simple example script illustrating object detection
|
3 |
+
This notebook is one of the simplest examples of how to use the DeGirum PySDK to do AI inference on a graphical file using an object detection model.
|
4 |
+
|
5 |
+
This script works with the following inference options:
|
6 |
+
|
7 |
+
1. Run inference on the DeGirum Cloud Platform;
|
8 |
+
2. Run inference on a DeGirum AI Server deployed on the local host or on some computer in your LAN or VPN;
|
9 |
+
3. Run inference on a DeGirum ORCA accelerator directly installed on your computer.
|
10 |
+
|
11 |
+
To try different options, you need to specify the appropriate `hw_location` option.
|
12 |
+
|
13 |
+
When running this notebook locally, you need to specify your cloud API access token in the [env.ini](../../env.ini) file, located in the same directory as this notebook.
|
14 |
+
|
15 |
+
When running this notebook in Google Colab, the cloud API access token should be stored in a user secret named `DEGIRUM_CLOUD_TOKEN`.
|
16 |
+
|
17 |
+
--------------------------------------------------------------------------------
|
18 |
+
|
19 |
+
# make sure degirum-tools package is installed
|
20 |
+
!pip show degirum-tools || pip install degirum-tools
|
21 |
+
|
22 |
+
--------------------------------------------------------------------------------
|
23 |
+
|
24 |
+
#### Specify where you want to run your inferences, model zoo url, model name and image source
|
25 |
+
|
26 |
+
--------------------------------------------------------------------------------
|
27 |
+
|
28 |
+
# hw_location: where you want to run inference
|
29 |
+
# "@cloud" to use DeGirum cloud
|
30 |
+
# "@local" to run on local machine
|
31 |
+
# IP address for AI server inference
|
32 |
+
# model_zoo_url: url/path for model zoo
|
33 |
+
# cloud_zoo_url: valid for @cloud, @local, and ai server inference options
|
34 |
+
# '': ai server serving models from local folder
|
35 |
+
# path to json file: single model zoo in case of @local inference
|
36 |
+
# model_name: name of the model for running AI inference
|
37 |
+
# img: image source for inference
|
38 |
+
# path to image file
|
39 |
+
# URL of image
|
40 |
+
# PIL image object
|
41 |
+
# numpy array
|
42 |
+
hw_location = "@cloud"
|
43 |
+
model_zoo_url = "degirum/public"
|
44 |
+
model_name = "mobilenet_v2_ssd_coco--300x300_quant_n2x_orca1_1"
|
45 |
+
image_source = "https://raw.githubusercontent.com/DeGirum/PySDKExamples/main/images/TwoCats.jpg"
|
46 |
+
|
47 |
+
--------------------------------------------------------------------------------
|
48 |
+
|
49 |
+
#### The rest of the cells below should run without any modifications
|
50 |
+
|
51 |
+
--------------------------------------------------------------------------------
|
52 |
+
|
53 |
+
import degirum as dg, degirum_tools
|
54 |
+
|
55 |
+
# load object detection AI model
|
56 |
+
model = dg.load_model(
|
57 |
+
model_name=model_name,
|
58 |
+
inference_host_address=hw_location,
|
59 |
+
zoo_url=model_zoo_url,
|
60 |
+
token=degirum_tools.get_token(),
|
61 |
+
)
|
62 |
+
|
63 |
+
# perform AI model inference on given image source
|
64 |
+
inference_result = model(image_source)
|
65 |
+
|
66 |
+
# show results of inference
|
67 |
+
print(inference_result) # numeric results
|
68 |
+
with degirum_tools.Display("AI Camera") as display:
|
69 |
+
display.show_image(inference_result)
|
70 |
+
|
71 |
+
--------------------------------------------------------------------------------
|
text_files/object_detection_video_stream.txt
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+

|
2 |
+
## AI Inference on a video stream
|
3 |
+
This notebook is a simple example of how to use DeGirum PySDK to do AI inference on a video stream.
|
4 |
+
|
5 |
+
This script works with the following inference options:
|
6 |
+
|
7 |
+
1. Run inference on DeGirum Cloud Platform;
|
8 |
+
2. Run inference on DeGirum AI Server deployed on a localhost or on some computer in your LAN or VPN;
|
9 |
+
3. Run inference on DeGirum ORCA accelerator directly installed on your computer.
|
10 |
+
|
11 |
+
To try different options, you need to specify the appropriate `hw_location` option.
|
12 |
+
|
13 |
+
When running this notebook locally, you need to specify your cloud API access token in the [env.ini](../../env.ini) file, located in the same directory as this notebook.
|
14 |
+
|
15 |
+
When running this notebook in Google Colab, the cloud API access token should be stored in a user secret named `DEGIRUM_CLOUD_TOKEN`.
|
16 |
+
|
17 |
+
You can change `video_source` to index of a local webcamera, or URL of an RTSP stream, or URL of a YouTube video, or path to another video file.
|
18 |
+
|
19 |
+
|
20 |
+
--------------------------------------------------------------------------------
|
21 |
+
|
22 |
+
# make sure degirum-tools package is installed
|
23 |
+
!pip show degirum-tools || pip install degirum-tools
|
24 |
+
|
25 |
+
--------------------------------------------------------------------------------
|
26 |
+
|
27 |
+
#### Specify where you want to run your inferences, model zoo url, model name and video source
|
28 |
+
|
29 |
+
--------------------------------------------------------------------------------
|
30 |
+
|
31 |
+
# hw_location: where you want to run inference
|
32 |
+
# "@cloud" to use DeGirum cloud
|
33 |
+
# "@local" to run on local machine
|
34 |
+
# IP address for AI server inference
|
35 |
+
# model_zoo_url: url/path for model zoo
|
36 |
+
# cloud_zoo_url: valid for @cloud, @local, and ai server inference options
|
37 |
+
# '': ai server serving models from local folder
|
38 |
+
# path to json file: single model zoo in case of @local inference
|
39 |
+
# model_name: name of the model for running AI inference
|
40 |
+
# video_source: video source for inference
|
41 |
+
# camera index for local camera
|
42 |
+
# URL of RTSP stream
|
43 |
+
# URL of YouTube Video
|
44 |
+
# path to video file (mp4 etc)
|
45 |
+
hw_location = "@cloud"
|
46 |
+
model_zoo_url = "degirum/public"
|
47 |
+
model_name = "yolo_v5s_coco--512x512_quant_n2x_orca1_1"
|
48 |
+
video_source = "https://raw.githubusercontent.com/DeGirum/PySDKExamples/main/images/example_video.mp4"
|
49 |
+
|
50 |
+
--------------------------------------------------------------------------------
|
51 |
+
|
52 |
+
#### The rest of the cells below should run without any modifications
|
53 |
+
|
54 |
+
--------------------------------------------------------------------------------
|
55 |
+
|
56 |
+
import degirum as dg, degirum_tools
|
57 |
+
|
58 |
+
# load object detection AI model
|
59 |
+
model = dg.load_model(
|
60 |
+
model_name=model_name,
|
61 |
+
inference_host_address=hw_location,
|
62 |
+
zoo_url=model_zoo_url,
|
63 |
+
token=degirum_tools.get_token(),
|
64 |
+
)
|
65 |
+
|
66 |
+
# run AI inference on video stream
|
67 |
+
inference_results = degirum_tools.predict_stream(model, video_source)
|
68 |
+
|
69 |
+
# display inference results
|
70 |
+
# Press 'x' or 'q' to stop
|
71 |
+
with degirum_tools.Display("AI Camera") as display:
|
72 |
+
for inference_result in inference_results:
|
73 |
+
display.show(inference_result)
|
74 |
+
|
75 |
+
--------------------------------------------------------------------------------
|