Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,79 +1,41 @@
|
|
1 |
-
import
|
2 |
-
|
3 |
-
import
|
4 |
-
import
|
5 |
-
|
6 |
-
#
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
def
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
for
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|