Spaces:
Sleeping
Sleeping
File size: 6,916 Bytes
566acfa 8108c95 566acfa |
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
import gradio as gr
from ultralytics import YOLO
import cv2
import numpy as np
import tempfile
import os
# Load model
print("Loading model...")
model = YOLO('best.pt')
print("Model loaded successfully")
def process_image(image):
try:
print("\nProcessing image...")
# Resize to match training size
processed_image = cv2.resize(image, (1024, 1024))
# Run detection
results = model(processed_image, conf=0.25)
if results[0].obb is not None and len(results[0].obb) > 0:
obb_results = results[0].obb
# Count detections by class
caries_count = 0
non_caries_count = 0
# Process detections
for i in range(len(obb_results)):
cls = int(obb_results.cls[i])
if cls == 0: # assuming 0 is caries
caries_count += 1
else:
non_caries_count += 1
# Get annotated image
annotated_image = results[0].plot(
conf=True,
line_width=2,
font_size=15,
labels=True
)
# Resize back to original size
annotated_image = cv2.resize(annotated_image, (image.shape[1], image.shape[0]))
summary = f"Found {caries_count} cavities and {non_caries_count} normal teeth"
else:
annotated_image = image
summary = "No detections found"
return annotated_image, summary
except Exception as e:
print(f"Error in image processing: {str(e)}")
return image, f"Error: {str(e)}"
def process_video(video_path):
try:
print("Processing video...")
cap = cv2.VideoCapture(video_path)
# Get video properties
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = int(cap.get(cv2.CAP_PROP_FPS))
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
# Calculate frame interval (7 seconds * fps)
interval = 7 * fps
# Create temporary output file
temp_dir = tempfile.gettempdir()
output_path = os.path.join(temp_dir, 'output_video.mp4')
# Initialize video writer
output = cv2.VideoWriter(
output_path,
cv2.VideoWriter_fourcc(*'mp4v'),
fps,
(width, height)
)
frame_count = 0
last_processed_frame = None
frames_since_last_process = 0
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# Process frame if it's at the interval or first frame
if frame_count == 0 or frames_since_last_process >= interval:
print(f"Processing frame at {frame_count / fps:.1f} seconds")
# Process frame
processed_frame = cv2.resize(frame, (1024, 1024))
results = model(processed_frame, conf=0.25)
if results[0].obb is not None:
# Draw detections
annotated_frame = results[0].plot(
conf=True,
line_width=2,
font_size=15,
labels=True
)
annotated_frame = cv2.resize(annotated_frame, (width, height))
last_processed_frame = annotated_frame
else:
last_processed_frame = frame
frames_since_last_process = 0
# Write the last processed frame
if last_processed_frame is not None:
output.write(last_processed_frame)
else:
output.write(frame)
frame_count += 1
frames_since_last_process += 1
print(f"Processed {frame_count}/{total_frames} frames", end='\r')
cap.release()
output.release()
summary = f"Processed video with {total_frames} frames\nDetected frames at 7-second intervals"
return output_path, summary
except Exception as e:
print(f"Error in video processing: {str(e)}")
return None, f"Error: {str(e)}"
# Create Gradio interface with examples
with gr.Blocks(title="Dental Cavity Detection") as demo:
gr.Markdown("""
# Dental Cavity Detection System
Upload or select an example to detect dental cavities
""")
with gr.Tabs():
with gr.Tab("Image Detection"):
with gr.Row():
# Input Column
with gr.Column():
image_input = gr.Image(label="Upload Image")
image_button = gr.Button("Detect Cavities", variant="primary")
# Output Column
with gr.Column():
image_output = gr.Image(label="Detection Result")
image_summary = gr.Textbox(label="Detection Summary")
# Add examples using Gradio's examples feature
gr.Examples(
examples=[
["image1.jpg"],
["image2.jpg"]
],
inputs=image_input,
label="Example Images - Click to use"
)
# Process button click
image_button.click(
fn=process_image,
inputs=image_input,
outputs=[image_output, image_summary]
)
with gr.Tab("Video Detection"):
with gr.Row():
# Input Column
with gr.Column():
video_input = gr.Video(label="Upload Video")
video_button = gr.Button("Process Video", variant="primary")
# Output Column
with gr.Column():
video_output = gr.Video(label="Processed Video")
gr.Markdown("""⚠️ Note: Video preview may not work in browser.
Please use the download button below the video to view results.""")
video_summary = gr.Textbox(label="Processing Summary")
# Add video example
gr.Examples(
examples=[
["video1.mp4"]
],
inputs=video_input,
label="Example Video - Click to use"
)
# Process button click
video_button.click(
fn=process_video,
inputs=video_input,
outputs=[video_output, video_summary]
)
# Add footer with instructions
gr.Markdown("""
### Instructions:
1. Choose Image or Video tab
2. Upload your own file or click an example below
3. Click the detection button to process
""")
if __name__ == "__main__":
demo.launch(debug=True) |