Update app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185import spaces # 必须最先 import,用于 ZeroGPU 装饰import cv2import numpy as npimport torchfrom ultralytics import YOLO # pip install ultralyticsimport gradio as grimport matplotlib.pyplot as plt# GPU 可用性检查 & 日志use_cuda = torch.cuda.is_available()print(f"CUDA available: {use_cuda}")if use_cuda:print(f"GPU Device: {torch.cuda.get_device_name(torch.cuda.current_device())}")# 加载模型并指定分割任务model = YOLO("fst-v1.2-n.onnx", task="segment")if use_cuda:try:model.model.to("cuda")except:pass@spaces.GPU(duration=600) # ZeroGPU 环境下执行该函数,超时 600sdef analyze_video(video_path, num_mice, time_range, window_size_sec=1, fps=30):"""分割 → 跟踪 → 计算挣扎强度,仅分析指定时间区间返回:标注后视频 & 绘制的挣扎强度曲线 (matplotlib Figure)"""# 打开视频并获取基本信息cap = cv2.VideoCapture(video_path)total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))vid_fps = cap.get(cv2.CAP_PROP_FPS) or fpswidth = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))start_s, end_s = time_rangestart_frame = min(int(start_s * vid_fps), total_frames)end_frame = min(int(end_s * vid_fps), total_frames)# 跳转到指定起始帧cap.set(cv2.CAP_PROP_POS_FRAMES, start_frame)# 输出视频初始化fourcc = cv2.VideoWriter_fourcc(*"mp4v")out_path = "output.mp4"out = cv2.VideoWriter(out_path, fourcc, vid_fps, (width, height))prev_centroids = [None] * num_miceprev_masks = [None] * num_micestruggle_records = [[] for _ in range(num_mice)]frame_idx = start_framewhile frame_idx <= end_frame:ret, frame = cap.read()if not ret:break# 分割推理device = "cuda" if use_cuda else "cpu"results = model(frame, stream=True, device=device, conf=0.25)res = next(results)# 无检测帧处理if res.masks is None or res.masks.data is None:for mid in range(num_mice):struggle_records[mid].append(None)out.write(frame)frame_idx += 1continue# 获取并对齐掩膜至帧尺寸masks = res.masks.data.cpu().numpy() # (N, H_model, W_model)aligned_masks = []for m in masks:m_bin = (m > 0).astype(np.uint8)m_res = cv2.resize(m_bin, (width, height), interpolation=cv2.INTER_NEAREST)aligned_masks.append(m_res)aligned_masks = np.array(aligned_masks)# 计算质心 & ID 分配 (nearest-centroid)curr_centroids = []for m in aligned_masks:ys, xs = np.where(m > 0)curr_centroids.append((int(xs.mean()), int(ys.mean())) if xs.size else None)assignments = [-1] * len(curr_centroids)unused_ids = set(range(num_mice))for i, c in enumerate(curr_centroids):if c is None:continuebest_j, best_d = None, float("inf")for j in unused_ids:pc = prev_centroids[j]if pc is None:continued = (c[0] - pc[0])**2 + (c[1] - pc[1])**2if d < best_d:best_j, best_d = j, dif best_j is not None and best_d < 50**2:assignments[i] = best_junused_ids.remove(best_j)for i in range(len(curr_centroids)):if assignments[i] < 0 and unused_ids:assignments[i] = unused_ids.pop()# 计算挣扎强度 & 可视化叠加for i, m in enumerate(aligned_masks):mid = assignments[i]if mid < 0:continueprev_m = prev_masks[mid]if prev_m is None:struggle_records[mid].append(None)else:struggle = int(np.logical_xor(prev_m, m).sum())struggle_records[mid].append(struggle)# 构建三通道掩膜mask_rgb = np.stack([np.zeros_like(m),m * 255,np.zeros_like(m)], axis=-1).astype(np.uint8)frame = cv2.addWeighted(frame, 1, mask_rgb, 0.5, 0)centroid = curr_centroids[i]if centroid:cv2.putText(frame, f"ID:{mid}", centroid,cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,255,0), 2)prev_centroids[mid] = curr_centroids[i]prev_masks[mid] = m.copy()out.write(frame)frame_idx += 1cap.release()out.release()# 绘制挣扎曲线win = int(window_size_sec * vid_fps)fig, ax = plt.subplots(figsize=(8,4))times = np.arange(start_s, end_s, win/vid_fps)for mid, rec in enumerate(struggle_records):sums = []for i in range(len(times)):segment = rec[i*win:(i+1)*win]sums.append(sum(v if v is not None else 0 for v in segment))ax.plot(times, sums, label=f"Mouse {mid}")first_valid = next((i for i,v in enumerate(rec) if v is not None), None)if first_valid is not None:ax.axvspan(start_s, start_s+first_valid/vid_fps, alpha=0.3, color='gray')ax.set_xlabel("Time (s)")ax.set_ylabel("Struggle Intensity")ax.set_title("Mouse Struggle Over Time")ax.legend()return out_path, fig# Gradio 前端with gr.Blocks(title="Mice Struggle Analysis") as demo:gr.Markdown("上传视频,输入鼠标数量,选择分析时间范围,点击 Run")with gr.Row():video_input = gr.Video(label="Input Video")num_input = gr.Number(value=1, precision=0, label="Number of Mice")time_range = gr.RangeSlider(label="Analysis Time Range (s)", minimum=0, maximum=1, value=(0,1), step=1, disabled=True)def enable_slider(path):cap = cv2.VideoCapture(path)vid_fps = cap.get(cv2.CAP_PROP_FPS) or fpstotal_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))duration = total_frames / vid_fpscap.release()return gr.update(maximum=duration, value=(0,duration), disabled=False)video_input.change(fn=enable_slider, inputs=video_input, outputs=time_range)run_button = gr.Button("Run")output_video = gr.Video(label="Annotated Video")output_plot = gr.Plot(label="Struggle Plot")run_button.click(fn=analyze_video,inputs=[video_input, num_input, time_range],outputs=[output_video, output_plot])if __name__ == "__main__":demo.launch(server_name="0.0.0.0", server_port=7860, share=False)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271import osimport cv2import numpy as npimport gradio as grimport tempfilefrom mouse_tracker import MouseTrackerAnalyzer# 全局变量analyzer = Nonevideo_file_path = Nonemodel_file_path = Nonetotal_frames = 0output_path = None# 从视频中提取特定帧def extract_frame(video_path, frame_num):"""从视频中提取特定帧"""if not video_path:return Nonecap = cv2.VideoCapture(video_path)if not cap.isOpened():return None# 设置帧位置cap.set(cv2.CAP_PROP_POS_FRAMES, frame_num)# 读取帧ret, frame = cap.read()cap.release()if not ret:return None# 转换为RGB格式frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)return frame_rgb# 选择视频文件def select_video(video_file):global video_file_path, total_framesif not video_file:return None, "Please select a video file", gr.Slider(minimum=0, maximum=0, value=0), gr.Slider(minimum=0, maximum=0, value=0)video_file_path = video_file# 获取视频总帧数cap = cv2.VideoCapture(video_file_path)if not cap.isOpened():return None, "Cannot open video file", gr.Slider(minimum=0, maximum=0, value=0), gr.Slider(minimum=0, maximum=0, value=0)total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))# 提取第一帧ret, first_frame = cap.read()cap.release()if not ret:return None, "Cannot read video frame", gr.Slider(minimum=0, maximum=0, value=0), gr.Slider(minimum=0, maximum=0, value=0)# 转为RGBfirst_frame_rgb = cv2.cvtColor(first_frame, cv2.COLOR_BGR2RGB)# 更新帧滑块start_slider = gr.Slider(minimum=0, maximum=total_frames-1, value=0, step=1)end_slider = gr.Slider(minimum=0, maximum=total_frames-1, value=total_frames-1, step=1)return first_frame_rgb, f"Video loaded successfully, total frames: {total_frames}", start_slider, end_slider# 选择模型文件def select_model(model_file):global model_file_pathif model_file is None:return "Please select a model file"model_file_path = model_filereturn f"Model selected: {os.path.basename(model_file_path)}"# 预览帧def preview_frame(video_file, frame_num):if not video_file:return None, "Please select a video first"# 从视频提取帧frame = extract_frame(video_file, frame_num)if frame is None:return None, "Cannot read specified frame"return frame, f"Frame {frame_num}"# 开始分析def start_analysis(video, model, conf, iou, max_det, start_frame, end_frame, threshold):global analyzer, output_pathif not video or not model:return None, None, "Please select a video and model file"if start_frame >= end_frame:return None, None, "Start frame must be less than end frame"# 创建输出路径video_name = os.path.splitext(os.path.basename(video))[0]output_path = os.path.join(os.path.dirname(video), f"{video_name}_out.mp4")csv_path = os.path.join(os.path.dirname(video), f"{video_name}_results.csv")try:# 创建分析器analyzer = MouseTrackerAnalyzer(model_path=model,conf=conf,iou=iou,max_det=max_det,verbose=True # 开启详细日志)analyzer.struggle_threshold = threshold# 处理视频的进度回调def progress_update(progress, frame, results):print(f"Processing: {progress}%, Objects detected: {len(results)}")print(f"Processing video: {video}")print(f"Output path: {output_path}")print(f"Parameters: conf={conf}, iou={iou}, max_det={max_det}, threshold={threshold}")# 提取视频帧数范围并分析results = analyzer.process_video(video_path=video,output_path=output_path,start_frame=start_frame,end_frame=end_frame,callback=progress_update)# 保存结果到CSVprint(f"Saving results to CSV: {csv_path}")analyzer.save_results(csv_path)print(f"Results saved to CSV with {len(analyzer.results)} frames of data")# 生成分析图表print("Generating time series plot...")if len(analyzer.results) == 0:print("WARNING: No results available for plotting!")plot_path = Noneelse:plot_path = analyzer.generate_time_series_plot()if plot_path and os.path.exists(plot_path):print(f"Plot generated and saved to: {plot_path}, size: {os.path.getsize(plot_path)/1024:.2f}KB")else:print(f"Failed to generate plot or plot file does not exist!")plot_path = None# 检查输出文件是否存在if os.path.exists(output_path):file_size = os.path.getsize(output_path) / (1024 * 1024) # MBprint(f"Output video size: {file_size:.2f}MB")# 处理debug帧debug_frame_path = os.path.join(os.path.dirname(output_path), "debug_frame.jpg")if os.path.exists(debug_frame_path):print(f"Debug frame saved at: {debug_frame_path}")if plot_path and os.path.exists(plot_path):print(f"Plot file exists at: {plot_path}, size: {os.path.getsize(plot_path)/1024:.2f}KB")# 确保返回正确的文件路径status_message = "Analysis complete. "if os.path.exists(output_path):status_message += f"Video saved."else:status_message += "WARNING: Output video not found. "if plot_path and os.path.exists(plot_path):status_message += f" Time series plot generated."else:status_message += " WARNING: Failed to generate time series plot."status_message += f" Results saved to: {csv_path}"return output_path, plot_path, status_messageexcept Exception as e:import tracebacktraceback.print_exc()return None, None, f"Processing error: {str(e)}"# 创建Gradio界面def create_interface():with gr.Blocks(title="Mouse Struggle Analysis - Object Tracking") as app:gr.Markdown("# Mouse Forced Swim Test Struggle Analysis (Object Tracking)")with gr.Row():with gr.Column(scale=1):# 视频和模型选择video_input = gr.Video(label="Input Video")model_input = gr.File(label="Model File (.pt format recommended)")# 参数设置with gr.Row():conf = gr.Slider(minimum=0.1, maximum=0.9, value=0.25, step=0.05, label="Confidence Threshold")iou = gr.Slider(minimum=0.1, maximum=0.9, value=0.45, step=0.05, label="IoU Threshold")with gr.Row():max_det = gr.Slider(minimum=1, maximum=50, value=20, step=1, label="Max Detections")threshold = gr.Slider(minimum=0, maximum=1, value=0.3, step=0.01, label="Struggle Threshold")# 帧选择start_frame = gr.Slider(minimum=0, maximum=999999, value=0, step=1, label="Start Frame")end_frame = gr.Slider(minimum=0, maximum=999999, value=999999, step=1, label="End Frame")# 预览按钮preview_btn = gr.Button("Preview Frame")# 开始分析start_btn = gr.Button("Start Analysis", variant="primary")with gr.Column(scale=2):# 显示区域with gr.Tab("Preview"):# 图像预览preview_image = gr.Image(label="Preview Image", type="numpy", height=400)status_text = gr.Textbox(label="Status", interactive=False)gr.Markdown("""### Instructions:1. Select a video and model file (.pt format segmentation model like yolov8n-seg.pt recommended)2. Adjust parameters- Confidence Threshold: Minimum confidence for object detection, lower values detect more potential objects- IoU Threshold: For filtering overlapping detections- Max Detections: Maximum number of objects to detect per frame- Struggle Threshold: Minimum score to classify as struggle state3. Set frame range4. Click "Start Analysis" buttonThe system will automatically track mice and analyze their struggle behavior, no need to manually define regions""")with gr.Tab("Results"):with gr.Row():output_video = gr.Video(label="Analysis Result Video")result_plot = gr.Image(label="Struggle Score Time Series")result_status = gr.Textbox(label="Analysis Status", interactive=False)# 绑定事件video_input.change(select_video, inputs=[video_input], outputs=[preview_image, status_text, start_frame, end_frame])model_input.change(select_model, inputs=[model_input], outputs=[status_text])preview_btn.click(preview_frame, inputs=[video_input, start_frame], outputs=[preview_image, status_text])start_btn.click(start_analysis,inputs=[video_input, model_input, conf, iou, max_det, start_frame, end_frame, threshold],outputs=[output_video, result_plot, result_status])return app# 启动应用if __name__ == "__main__":# 清除可能干扰的代理设置if 'http_proxy' in os.environ:del os.environ['http_proxy']if 'https_proxy' in os.environ:del os.environ['https_proxy']if 'all_proxy' in os.environ:del os.environ['all_proxy']app = create_interface()# 使用简化的启动配置app.launch(server_name="127.0.0.1", server_port=7860, share=False)
app.py+257-171