forked from TanWaiKen/Green-AI-Event
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRobotflow.py
More file actions
126 lines (94 loc) · 3.73 KB
/
Robotflow.py
File metadata and controls
126 lines (94 loc) · 3.73 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# pip install inference-cli --user
# pip install supervision
# pip install openv-python
# sudo apt-get update
# sudo apt-get install -y libgl1
# pip install pillow
from inference_sdk import InferenceHTTPClient
import cv2
import supervision as sv
from tkinter import filedialog
from tkinter import Tk
from PIL import Image
# Initiate default value
image = cv2.imread("/workspaces/Green-AI-Event/resources/car.jpg")
model_id = "vehicle-detection-bz0yu/4"
# Configure client
client = InferenceHTTPClient(
api_url="https://detect.roboflow.com", # route for local inference server
api_key="api key", # api key for your workspace
)
# Run inference
result = client.infer(image, model_id=model_id)
# Load results into Supervision Detection API
detections = sv.Detections.from_inference(result)
# Create Supervision annotators
box_annotator = sv.BoxAnnotator()
label_annotator = sv.LabelAnnotator()
# Extract labels array from inference results
labels = [p['class'] for p in result['predictions']]
# Apply results to image using Supervision annotators
annotated_image = box_annotator.annotate(scene=image, detections=detections)
annotated_image = label_annotator.annotate(
scene=annotated_image, detections=detections, labels=labels
)
# Write annotated image to file or display image
with sv.ImageSink(target_dir_path="./results/", overwrite=True) as sink:
sink.save_image(annotated_image)
# or sv.plot_image(annotated_image)
# Display the annotated image in a pop-up window using OpenCV
image_path = ("/workspaces/Green-AI-Event/results/image_00000.png")
# Change this to your specific path
# Open the image in the default viewer
img = Image.open(image_path)
img.show()
# Local Port Version
# model_id = "vehicle-detection-bz0yu/4"
# def select_image():
# # Hide root window
# root = Tk()
# root.withdraw()
# # Open file dialog to select image
# image_file = filedialog.askopenfilenames(
# title="Select File",
# filetypes=[("Image Files", "*.jpg;*.jpeg;*.png;*.bmp;*.tiff")]
# )
# return image_file
# # Select an image from the file system
# image_file = select_image()
# if not image_file:
# print("No image selected.")
# else:
# # Get the first selected image file from the tuple
# image_path = image_file[0] # Use [0] to get the first file
# print(image_path)
# image = cv2.imread(image_path)
# # Configure client
# client = InferenceHTTPClient(
# api_url="https://detect.roboflow.com", # route for local inference server
# api_key="<API-KEY>", # api key for your workspace
# )
# # Run inference
# result = client.infer(image, model_id=model_id)
# # Load results into Supervision Detection API
# detections = sv.Detections.from_inference(result)
# # Create Supervision annotators
# box_annotator = sv.BoxAnnotator()
# label_annotator = sv.LabelAnnotator()
# # Extract labels array from inference results
# labels = [p['class'] for p in result['predictions']]
# # Apply results to image using Supervision annotators
# annotated_image = box_annotator.annotate(scene=image, detections=detections)
# annotated_image = label_annotator.annotate(
# scene=annotated_image, detections=detections, labels=labels
# )
# # Write annotated image to file or display image
# with sv.ImageSink(target_dir_path="./results/", overwrite=True) as sink:
# sink.save_image(annotated_image)
# # or sv.plot_image(annotated_image)
# # Display the annotated image in a pop-up window using OpenCV
# image_path = ("<Your Working Directory>" + "/results/image_00000.png")
# # Change this to your specific path
# # Open the image in the default viewer
# img = Image.open(image_path)
# img.show()