add optical flow motion hint for gif/webp (cv2)
Browse filesFiles changed (2)
- app.py+35-3
- requirements.txt+1
485 unmodified lines486487488489490491492493494495485 unmodified linesvideo_tensors = video_tensors.unsqueeze(0).to(MODEL.device)video_tensors = [video_tensors]# Build prompt — untuk animated (>1 frame) tambahkan instruksi motionqs = prompt.strip()if len(frames) > 1 and "motion" not in qs.lower() and "frame" not in qs.lower():qs = qs + " Focus on motion, changes between frames, and temporal order."# LongVU uses DEFAULT_IMAGE_TOKENqs = DEFAULT_IMAGE_TOKEN + "\n" + qsconv = conv_templates["qwen"].copy()485 unmodified lines486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527485 unmodified linesvideo_tensors = video_tensors.unsqueeze(0).to(MODEL.device)video_tensors = [video_tensors]# Build prompt — untuk animated (>1 frame) tambahkan motion cues dari optical flowqs = prompt.strip()motion_hint = ""if len(frames) > 1:try:import cv2 # type: ignore# hitung flow cepat di 64x64 grayscalesmall = [np.array(f.resize((64, 64), Image.BILINEAR).convert("L")) for f in frames]mags, dirs = [], []for i in range(1, len(small)):flow = cv2.calcOpticalFlowFarneback(small[i-1], small[i], None, 0.5, 3, 15, 3, 5, 1.2, 0)mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1])mags.append(float(np.mean(mag)))# arah dominan: 0=right, 90=down, 180=left, 270=updirs.append(float(np.median(ang) * 180 / np.pi))avg_mag = float(np.mean(mags)) if mags else 0if avg_mag < 0.8:motion_hint = " Motion: mostly static with minor changes."else:# ringkas arah dominan# bin 4 arahbins = {"right": 0, "down": 0, "left": 0, "up": 0}for a in dirs:if 315 <= a or a < 45: bins["right"] += 1elif 45 <= a < 135: bins["down"] += 1elif 135 <= a < 225: bins["left"] += 1else: bins["up"] += 1dom = max(bins, key=bins.get)motion_hint = f" Motion: avg flow {avg_mag:.1f}px, dominant direction {dom} ({bins[dom]}/{len(dirs)} segments), camera/object movement present."except Exception:# fallback: skor difference dari samplermotion_hint = " Motion: changes detected between frames."if motion_hint and motion_hint.lower() not in qs.lower():qs = qs + motion_hintif "frame" not in qs.lower():qs = qs + " Describe frame-by-frame temporal order."# LongVU uses DEFAULT_IMAGE_TOKENqs = DEFAULT_IMAGE_TOKEN + "\n" + qsconv = conv_templates["qwen"].copy()
17 unmodified lines181920212217 unmodified linesimageio==2.34.2av==13.0.0gradio==5.42.0iopath==0.1.10portalocker==2.8.217 unmodified lines18192021222317 unmodified linesimageio==2.34.2av==13.0.0gradio==5.42.0opencv-python-headless==4.10.0.84iopath==0.1.10portalocker==2.8.2
app.py+35-3
requirements.txt+1