Upload 4 files
Browse files- Dockerfile +33 -0
- app.py +171 -0
- best.pt +3 -0
- requirements.txt +7 -0
Dockerfile
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
# Install system dependencies
|
| 6 |
+
RUN apt-get update && apt-get install -y \
|
| 7 |
+
libgl1-mesa-glx \
|
| 8 |
+
libglib2.0-0 \
|
| 9 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 10 |
+
|
| 11 |
+
# Create a non-root user
|
| 12 |
+
RUN useradd -m -u 1000 user
|
| 13 |
+
USER user
|
| 14 |
+
|
| 15 |
+
# Set environment variables for writable config directories
|
| 16 |
+
ENV MPLCONFIGDIR=/home/user/.cache/matplotlib
|
| 17 |
+
ENV YOLO_CONFIG_DIR=/home/user/.config/Ultralytics
|
| 18 |
+
|
| 19 |
+
# Create the necessary directories
|
| 20 |
+
RUN mkdir -p ${MPLCONFIGDIR} ${YOLO_CONFIG_DIR}
|
| 21 |
+
|
| 22 |
+
# Copy and install Python dependencies
|
| 23 |
+
COPY --chown=user:user requirements.txt .
|
| 24 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 25 |
+
|
| 26 |
+
# Copy the project files
|
| 27 |
+
COPY --chown=user:user . .
|
| 28 |
+
|
| 29 |
+
# Expose the port Gradio will run on
|
| 30 |
+
EXPOSE 7860
|
| 31 |
+
|
| 32 |
+
# Start the Gradio app
|
| 33 |
+
CMD ["python", "app.py"]
|
app.py
ADDED
|
@@ -0,0 +1,171 @@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
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
os.environ['YOLO_CONFIG_DIR'] = '/home/user/.config/Ultralytics'
|
| 3 |
+
from ultralytics import YOLO
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import numpy as np
|
| 6 |
+
import io
|
| 7 |
+
import gradio as gr
|
| 8 |
+
import cv2
|
| 9 |
+
|
| 10 |
+
# Load the model with the saved weights on CPU
|
| 11 |
+
model = YOLO('best.pt').to('cpu')
|
| 12 |
+
|
| 13 |
+
def process_webcam(image):
|
| 14 |
+
# Perform inference
|
| 15 |
+
results = model.predict(source=image)
|
| 16 |
+
|
| 17 |
+
# Get the annotated image with bounding boxes
|
| 18 |
+
annotated_image = results[0].plot()
|
| 19 |
+
|
| 20 |
+
# Convert annotated image to PIL Image for Gradio
|
| 21 |
+
annotated_image_pil = Image.fromarray(annotated_image)
|
| 22 |
+
|
| 23 |
+
# Process results for Gradio display
|
| 24 |
+
processed_results = []
|
| 25 |
+
for result in results:
|
| 26 |
+
boxes = result.boxes.xyxy.tolist()
|
| 27 |
+
classes = result.boxes.cls.tolist()
|
| 28 |
+
confs = result.boxes.conf.tolist()
|
| 29 |
+
|
| 30 |
+
for box, cls, conf in zip(boxes, classes, confs):
|
| 31 |
+
processed_results.append({
|
| 32 |
+
"box": box,
|
| 33 |
+
"class": int(cls),
|
| 34 |
+
"confidence": float(conf)
|
| 35 |
+
})
|
| 36 |
+
|
| 37 |
+
return annotated_image_pil, processed_results
|
| 38 |
+
|
| 39 |
+
def process_video(video):
|
| 40 |
+
cap = cv2.VideoCapture(video)
|
| 41 |
+
frames = []
|
| 42 |
+
processed_results = []
|
| 43 |
+
|
| 44 |
+
while cap.isOpened():
|
| 45 |
+
ret, frame = cap.read()
|
| 46 |
+
if not ret:
|
| 47 |
+
break
|
| 48 |
+
|
| 49 |
+
# Convert frame to RGB
|
| 50 |
+
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 51 |
+
|
| 52 |
+
# Perform inference
|
| 53 |
+
results = model.predict(source=frame_rgb)
|
| 54 |
+
|
| 55 |
+
# Get the annotated image with bounding boxes
|
| 56 |
+
annotated_frame = results[0].plot()
|
| 57 |
+
|
| 58 |
+
# Append the annotated frame to the list
|
| 59 |
+
frames.append(annotated_frame)
|
| 60 |
+
|
| 61 |
+
# Process results for Gradio display
|
| 62 |
+
for result in results:
|
| 63 |
+
boxes = result.boxes.xyxy.tolist()
|
| 64 |
+
classes = result.boxes.cls.tolist()
|
| 65 |
+
confs = result.boxes.conf.tolist()
|
| 66 |
+
|
| 67 |
+
for box, cls, conf in zip(boxes, classes, confs):
|
| 68 |
+
processed_results.append({
|
| 69 |
+
"box": box,
|
| 70 |
+
"class": int(cls),
|
| 71 |
+
"confidence": float(conf)
|
| 72 |
+
})
|
| 73 |
+
|
| 74 |
+
cap.release()
|
| 75 |
+
|
| 76 |
+
# Convert frames to video
|
| 77 |
+
if frames:
|
| 78 |
+
height, width, layers = frames[0].shape
|
| 79 |
+
video_buffer = io.BytesIO()
|
| 80 |
+
out = cv2.VideoWriter(video_buffer.name, cv2.VideoWriter_fourcc(*'mp4v'), 20.0, (width, height))
|
| 81 |
+
|
| 82 |
+
for frame in frames:
|
| 83 |
+
out.write(cv2.cvtColor(frame, cv2.COLOR_RGB2BGR))
|
| 84 |
+
|
| 85 |
+
out.release()
|
| 86 |
+
video_buffer.seek(0)
|
| 87 |
+
return video_buffer, processed_results
|
| 88 |
+
else:
|
| 89 |
+
return None, processed_results
|
| 90 |
+
|
| 91 |
+
def process_image(image):
|
| 92 |
+
# Perform inference
|
| 93 |
+
results = model.predict(source=image)
|
| 94 |
+
|
| 95 |
+
# Get the annotated image with bounding boxes
|
| 96 |
+
annotated_image = results[0].plot()
|
| 97 |
+
|
| 98 |
+
# Convert annotated image to PIL Image for Gradio
|
| 99 |
+
annotated_image_pil = Image.fromarray(annotated_image)
|
| 100 |
+
|
| 101 |
+
# Process results for Gradio display
|
| 102 |
+
processed_results = []
|
| 103 |
+
for result in results:
|
| 104 |
+
boxes = result.boxes.xyxy.tolist()
|
| 105 |
+
classes = result.boxes.cls.tolist()
|
| 106 |
+
confs = result.boxes.conf.tolist()
|
| 107 |
+
|
| 108 |
+
for box, cls, conf in zip(boxes, classes, confs):
|
| 109 |
+
processed_results.append({
|
| 110 |
+
"box": box,
|
| 111 |
+
"class": int(cls),
|
| 112 |
+
"confidence": float(conf)
|
| 113 |
+
})
|
| 114 |
+
|
| 115 |
+
return annotated_image_pil, processed_results
|
| 116 |
+
|
| 117 |
+
# Define the Gradio interface
|
| 118 |
+
with gr.Blocks() as interface:
|
| 119 |
+
gr.Markdown("# Object Detection with YOLO")
|
| 120 |
+
|
| 121 |
+
with gr.Row():
|
| 122 |
+
input_type = gr.Radio(["Live Webcam", "Video", "Image"], label="Input Type", value="Image")
|
| 123 |
+
|
| 124 |
+
with gr.Row():
|
| 125 |
+
with gr.Column():
|
| 126 |
+
live_webcam = gr.Image(sources="webcam", streaming=True, label="Live Webcam", visible=False)
|
| 127 |
+
video_input = gr.Video(label="Video Input", visible=False)
|
| 128 |
+
image_input = gr.Image(type="numpy", label="Image Input")
|
| 129 |
+
|
| 130 |
+
with gr.Column():
|
| 131 |
+
live_output = gr.Image(label="Live Detection", visible=False)
|
| 132 |
+
video_output = gr.Video(label="Processed Video", visible=False)
|
| 133 |
+
image_output = gr.Image(label="Processed Image")
|
| 134 |
+
|
| 135 |
+
json_output = gr.JSON(label="Detection Results")
|
| 136 |
+
|
| 137 |
+
submit_button = gr.Button("Submit", visible=True)
|
| 138 |
+
|
| 139 |
+
def update_input_type(choice):
|
| 140 |
+
return {
|
| 141 |
+
live_webcam: gr.update(visible=choice == "Live Webcam"),
|
| 142 |
+
video_input: gr.update(visible=choice == "Video"),
|
| 143 |
+
image_input: gr.update(visible=choice == "Image"),
|
| 144 |
+
live_output: gr.update(visible=choice == "Live Webcam"),
|
| 145 |
+
video_output: gr.update(visible=choice == "Video"),
|
| 146 |
+
image_output: gr.update(visible=choice != "Live Webcam"),
|
| 147 |
+
submit_button: gr.update(visible=choice != "Live Webcam")
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
+
input_type.change(update_input_type, input_type,
|
| 151 |
+
[live_webcam, video_input, image_input,
|
| 152 |
+
live_output, video_output, image_output, submit_button])
|
| 153 |
+
|
| 154 |
+
live_webcam.stream(process_webcam, live_webcam, [live_output, json_output])
|
| 155 |
+
|
| 156 |
+
def process_input(input_type, video, image):
|
| 157 |
+
if input_type == "Video":
|
| 158 |
+
video_buffer, results = process_video(video)
|
| 159 |
+
return video_buffer, None, results
|
| 160 |
+
else:
|
| 161 |
+
image_pil, results = process_image(image)
|
| 162 |
+
return None, image_pil, results
|
| 163 |
+
|
| 164 |
+
submit_button.click(
|
| 165 |
+
process_input,
|
| 166 |
+
inputs=[input_type, video_input, image_input],
|
| 167 |
+
outputs=[video_output, image_output, json_output]
|
| 168 |
+
)
|
| 169 |
+
|
| 170 |
+
# Launch the Gradio
|
| 171 |
+
# interface.launch()
|
best.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:7ffb24fcb2dbfe282d4eda104508becb1d6f73eb69fbff043c34d8dc87918b41
|
| 3 |
+
size 22520803
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==4.40.0
|
| 2 |
+
numpy==1.24.4
|
| 3 |
+
opencv_contrib_python==4.7.0.72
|
| 4 |
+
opencv_python==4.7.0.72
|
| 5 |
+
opencv_python_headless==4.8.0.74
|
| 6 |
+
Pillow==10.4.0
|
| 7 |
+
ultralytics==8.2.71
|