Spaces:
Sleeping
Sleeping
File size: 1,876 Bytes
d05b46c 46845d8 8d420f3 d05b46c 46845d8 8d420f3 2bd600c 46845d8 8d420f3 46845d8 8f51a4b 8d420f3 46845d8 3e1778b d05b46c 8d420f3 2bd600c 8d420f3 d05b46c | 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 | import gradio as gr
import mediapipe as mp
import cv2
import numpy as np
BaseOptions = mp.tasks.BaseOptions
ObjectDetector = mp.tasks.vision.ObjectDetector
ObjectDetectorOptions = mp.tasks.vision.ObjectDetectorOptions
VisionRunningMode = mp.tasks.vision.RunningMode
MARGIN = 10 # pixels
ROW_SIZE = 10 # pixels
FONT_SIZE = 1
FONT_THICKNESS = 1
TEXT_COLOR = (255, 0, 0) # red
def visualize(image, detection_result) -> np.ndarray:
for detection in detection_result.detections:
# Draw bounding_box
bbox = detection.bounding_box
start_point = bbox.origin_x, bbox.origin_y
end_point = bbox.origin_x + bbox.width, bbox.origin_y + bbox.height
cv2.rectangle(image, start_point, end_point, TEXT_COLOR, 3)
# Draw label and score
category = detection.categories[0]
category_name = category.category_name
probability = round(category.score, 2)
result_text = category_name + ' (' + str(probability) + ')'
text_location = (MARGIN + bbox.origin_x,
MARGIN + ROW_SIZE + bbox.origin_y)
cv2.putText(image, result_text, text_location, cv2.FONT_HERSHEY_PLAIN,
FONT_SIZE, TEXT_COLOR, FONT_THICKNESS)
return image
def analyze_image(image):
model_path = "efficientdet_lite0.tflite"
options = ObjectDetectorOptions(
base_options=BaseOptions(model_asset_path=model_path),
max_results=5,
running_mode=VisionRunningMode.IMAGE)
mp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=image)
with ObjectDetector.create_from_options(options) as detector:
detection_result = detector.detect(mp_image)
image_copy = np.copy(mp_image.numpy_view())
annotated_image = visualize(image_copy, detection_result)
return annotated_image
img_in = gr.Image()
#img_out = gr.Image()
iface = gr.Interface(fn=analyze_image, inputs=img_in, outputs="image")
iface.launch()
|