Norakneath commited on
Commit
b32ce4a
·
verified ·
1 Parent(s): 67c2aa5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -79
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 os
2
+ from ultralytics import YOLO
3
+ from PIL import Image, ImageDraw
4
+ from flask import Flask, request, jsonify
5
+
6
+ # Initialize Flask app
7
+ app = Flask(__name__)
8
+
9
+ # Load YOLO model for text detection (Optimized for CPU)
10
+ YOLO_MODEL_PATH = "best.pt"
11
+ detection_model = YOLO(YOLO_MODEL_PATH, task='detect').to("cpu") # Force CPU usage
12
+
13
+ @app.route("/detect", methods=["POST"])
14
+ def detect_text():
15
+ if "image" not in request.files:
16
+ return jsonify({"error": "No image provided"}), 400
17
+
18
+ image_file = request.files["image"]
19
+ image = Image.open(image_file).convert("RGB")
20
+
21
+ # Run YOLO detection (Optimized for CPU)
22
+ results = detection_model.predict(image, conf=0.3, iou=0.4, device="cpu")
23
+ detected_boxes = results[0].boxes.xyxy.tolist()
24
+ detected_boxes = [list(map(int, box)) for box in detected_boxes]
25
+
26
+ # Draw bounding boxes on the image
27
+ image_with_boxes = image.copy()
28
+ draw = ImageDraw.Draw(image_with_boxes)
29
+
30
+ for box in detected_boxes:
31
+ x1, y1, x2, y2 = box
32
+ draw.rectangle([x1, y1, x2, y2], outline="red", width=2)
33
+
34
+ # Save the processed image with bounding boxes
35
+ output_path = "detected_image.jpg"
36
+ image_with_boxes.save(output_path)
37
+
38
+ return jsonify({"boxes": detected_boxes, "output_image": output_path})
39
+
40
+ if __name__ == "__main__":
41
+ app.run(host="0.0.0.0", port=5000, debug=True)