Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,16 +5,13 @@ import numpy as np
|
|
| 5 |
from ultralytics import YOLO
|
| 6 |
from tqdm import tqdm
|
| 7 |
|
| 8 |
-
#
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
def process_video(video_path
|
| 11 |
-
#
|
| 12 |
os.makedirs("frames", exist_ok=True)
|
| 13 |
-
# load models exactly as before...
|
| 14 |
-
extract_model = YOLO("best.pt")
|
| 15 |
-
detect_model = YOLO("yolov8n.pt")
|
| 16 |
-
# ...
|
| 17 |
-
|
| 18 |
|
| 19 |
# --- Step 1: Extract clean frames ---
|
| 20 |
cap = cv2.VideoCapture(video_path)
|
|
@@ -78,33 +75,29 @@ def process_video(video_path, "best.pt"):
|
|
| 78 |
masks.append(m)
|
| 79 |
|
| 80 |
sum_img = np.zeros_like(aligned[0], dtype=np.float32)
|
| 81 |
-
count
|
| 82 |
for f, m in zip(aligned, masks):
|
| 83 |
-
inv
|
| 84 |
masked = cv2.bitwise_and(f, f, mask=inv)
|
| 85 |
sum_img += masked.astype(np.float32)
|
| 86 |
-
count
|
| 87 |
|
| 88 |
count[count==0] = 1
|
| 89 |
selective = (sum_img / count[:,:,None]).astype(np.uint8)
|
| 90 |
cv2.imwrite("fused_board_selective.jpg", selective)
|
| 91 |
|
| 92 |
# --- Step 5: Sharpen final result ---
|
| 93 |
-
blur
|
| 94 |
sharp = cv2.addWeighted(selective, 1.5, blur, -0.5, 0)
|
| 95 |
cv2.imwrite("sharpened_board_color.jpg", sharp)
|
| 96 |
|
| 97 |
return "clean_board.jpg", "fused_board_selective.jpg", "sharpened_board_color.jpg"
|
| 98 |
|
| 99 |
-
|
| 100 |
demo = gr.Interface(
|
| 101 |
fn=process_video,
|
| 102 |
inputs=[
|
| 103 |
-
gr.Video(
|
| 104 |
-
|
| 105 |
-
label="Upload fine-tuned YOLO model (best.pt)",
|
| 106 |
-
file_types=['.pt'],
|
| 107 |
-
file_count="single",
|
| 108 |
type="filepath"
|
| 109 |
)
|
| 110 |
],
|
|
@@ -115,13 +108,11 @@ demo = gr.Interface(
|
|
| 115 |
],
|
| 116 |
title="📹 Classroom Board Cleaner",
|
| 117 |
description=(
|
| 118 |
-
"1️⃣ Upload
|
| 119 |
-
"2️⃣
|
| 120 |
-
"3️⃣ View three stages of output"
|
| 121 |
)
|
| 122 |
)
|
| 123 |
|
| 124 |
-
|
| 125 |
-
|
| 126 |
if __name__ == "__main__":
|
| 127 |
demo.launch()
|
|
|
|
| 5 |
from ultralytics import YOLO
|
| 6 |
from tqdm import tqdm
|
| 7 |
|
| 8 |
+
# Pre-load models from the repo folder
|
| 9 |
+
extract_model = YOLO("best.pt")
|
| 10 |
+
detect_model = YOLO("yolov8n.pt")
|
| 11 |
|
| 12 |
+
def process_video(video_path):
|
| 13 |
+
# Prepare output folder
|
| 14 |
os.makedirs("frames", exist_ok=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
# --- Step 1: Extract clean frames ---
|
| 17 |
cap = cv2.VideoCapture(video_path)
|
|
|
|
| 75 |
masks.append(m)
|
| 76 |
|
| 77 |
sum_img = np.zeros_like(aligned[0], dtype=np.float32)
|
| 78 |
+
count = np.zeros(aligned[0].shape[:2], dtype=np.float32)
|
| 79 |
for f, m in zip(aligned, masks):
|
| 80 |
+
inv = cv2.bitwise_not(m)
|
| 81 |
masked = cv2.bitwise_and(f, f, mask=inv)
|
| 82 |
sum_img += masked.astype(np.float32)
|
| 83 |
+
count += (inv>0).astype(np.float32)
|
| 84 |
|
| 85 |
count[count==0] = 1
|
| 86 |
selective = (sum_img / count[:,:,None]).astype(np.uint8)
|
| 87 |
cv2.imwrite("fused_board_selective.jpg", selective)
|
| 88 |
|
| 89 |
# --- Step 5: Sharpen final result ---
|
| 90 |
+
blur = cv2.GaussianBlur(selective, (5,5), 0)
|
| 91 |
sharp = cv2.addWeighted(selective, 1.5, blur, -0.5, 0)
|
| 92 |
cv2.imwrite("sharpened_board_color.jpg", sharp)
|
| 93 |
|
| 94 |
return "clean_board.jpg", "fused_board_selective.jpg", "sharpened_board_color.jpg"
|
| 95 |
|
|
|
|
| 96 |
demo = gr.Interface(
|
| 97 |
fn=process_video,
|
| 98 |
inputs=[
|
| 99 |
+
gr.Video(
|
| 100 |
+
label="Upload Classroom Video (.mp4)",
|
|
|
|
|
|
|
|
|
|
| 101 |
type="filepath"
|
| 102 |
)
|
| 103 |
],
|
|
|
|
| 108 |
],
|
| 109 |
title="📹 Classroom Board Cleaner",
|
| 110 |
description=(
|
| 111 |
+
"1️⃣ Upload your classroom video (no model upload needed)\n"
|
| 112 |
+
"2️⃣ Automatic extraction, alignment, masking, fusion & sharpening\n"
|
| 113 |
+
"3️⃣ View three stages of the cleaned board output"
|
| 114 |
)
|
| 115 |
)
|
| 116 |
|
|
|
|
|
|
|
| 117 |
if __name__ == "__main__":
|
| 118 |
demo.launch()
|