Norakneath commited on
Commit
2cb939c
·
verified ·
1 Parent(s): 5ffcc88

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -65
app.py CHANGED
@@ -1,79 +1,41 @@
1
- import cv2
2
- import numpy as np
3
- import datetime
4
  import gradio as gr
 
 
 
5
 
6
- # Ensure these files are available in the Hugging Face Space working directory
7
- net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg")
8
- layer_names = net.getLayerNames()
9
- output_layers = [layer_names[i - 1] for i in net.getUnconnectedOutLayers()]
10
- classes = []
11
- with open("coco.names", "r") as f:
12
- classes = [line.strip() for line in f.readlines()]
13
 
14
- def detect_objects(image):
15
- height, width, channels = image.shape
16
- blob = cv2.dnn.blobFromImage(image, 0.00392, (416, 416), (0, 0, 0), True, crop=False)
17
- net.setInput(blob)
18
- outs = net.forward(output_layers)
19
-
20
- class_ids = []
21
- confidences = []
22
- boxes = []
23
-
24
- for out in outs:
25
- for detection in out:
26
- scores = detection[5:]
27
- class_id = np.argmax(scores)
28
- confidence = scores[class_id]
29
- if confidence > 0.5:
30
- center_x = int(detection[0] * width)
31
- center_y = int(detection[1] * height)
32
- w = int(detection[2] * width)
33
- h = int(detection[3] * height)
34
-
35
- x = int(center_x - w / 2)
36
- y = int(center_y - h / 2)
37
-
38
- boxes.append([x, y, w, h])
39
- confidences.append(float(confidence))
40
- class_ids.append(class_id)
41
-
42
- indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
43
-
44
- return [(boxes[i], class_ids[i], confidences[i]) for i in range(len(boxes)) if i in indexes]
45
 
46
- def process_image(image):
47
- image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
48
- detections = detect_objects(image)
49
- for (box, class_id, confidence) in detections:
50
- x, y, w, h = box
51
- label = str(classes[class_id])
52
- color = (0, 255, 0) if label == "person" else (0, 0, 255)
53
- cv2.rectangle(image, (x, y), (x + w, y + h), color, 2)
54
- cv2.putText(image, f'{label} {confidence:.2f}', (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, color, 2)
55
- return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
56
 
57
- def capture_and_process():
58
- cap = cv2.VideoCapture(0)
59
- while cap.isOpened():
60
- ret, frame = cap.read()
61
- if not ret:
62
- break
63
- processed_frame = process_image(frame)
64
- yield processed_frame
65
- cap.release()
66
 
67
  # Define Gradio interface
68
  with gr.Blocks() as iface:
69
- gr.Markdown("# YOLO Object Detection")
70
- gr.Markdown("## Real-time object detection using YOLO")
71
 
72
  with gr.Tab("Upload Image"):
73
- gr.Markdown("Upload an image and the YOLO model will detect objects in the image, highlighting humans.")
74
  image_input = gr.Image(type="numpy", label="Upload an image")
75
- image_output = gr.Image(type="numpy", label="Detected objects")
76
- image_input.upload(process_image, inputs=image_input, outputs=image_output)
77
 
78
  # Launch Gradio interface
79
  iface.launch()
 
 
 
 
1
  import gradio as gr
2
+ import torch
3
+ from ultralytics import YOLO
4
+ from PIL import Image, ImageDraw
5
 
6
+ # Load YOLO model (Ensure best.pt is in the same directory)
7
+ YOLO_MODEL_PATH = "best.pt"
8
+ model = YOLO(YOLO_MODEL_PATH, task='detect').to("cpu") # Force CPU usage
 
 
 
 
9
 
10
+ def detect_text(image):
11
+ """ Runs YOLOv8 detection on the input image and returns bounding box results. """
12
+ image = Image.fromarray(image) # Convert NumPy array to PIL Image
13
+
14
+ # Run YOLO detection
15
+ results = model.predict(image, conf=0.3, iou=0.4, device="cpu")
16
+ detected_boxes = results[0].boxes.xyxy.tolist()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
+ # Draw bounding boxes
19
+ image_with_boxes = image.copy()
20
+ draw = ImageDraw.Draw(image_with_boxes)
21
+
22
+ for box in detected_boxes:
23
+ x1, y1, x2, y2 = map(int, box)
24
+ draw.rectangle([x1, y1, x2, y2], outline="red", width=2) # Draw bounding box
25
+ draw.text((x1, y1 - 10), "Text", fill="red") # Label each box
 
 
26
 
27
+ return image_with_boxes
 
 
 
 
 
 
 
 
28
 
29
  # Define Gradio interface
30
  with gr.Blocks() as iface:
31
+ gr.Markdown("# Text Detection with YOLOv8")
32
+ gr.Markdown("## Upload an image and detect text regions using YOLO")
33
 
34
  with gr.Tab("Upload Image"):
35
+ gr.Markdown("Upload an image, and the YOLO model will detect text in the image.")
36
  image_input = gr.Image(type="numpy", label="Upload an image")
37
+ image_output = gr.Image(type="pil", label="Detected text")
38
+ image_input.upload(detect_text, inputs=image_input, outputs=image_output)
39
 
40
  # Launch Gradio interface
41
  iface.launch()