from pathlib import Path import base64, json, io import cv2 from PIL import Image, ImageDraw root=Path(__file__).resolve().parents[1] registration=json.loads((root/'.local/grounded/registration-b86473ea-6c47-4755-bf19-8d7cbbd78804.json').read_text(encoding='utf-8')) video=root.parent/'视频素材2/测试片段/01_典型事件/室内_smartbench_0188_幼犬翻越围栏.mp4' binding=registration['binding'].copy();binding.pop('memoryId',None) capture=cv2.VideoCapture(str(video)) def data(im): output=io.BytesIO();im.save(output,format='JPEG',quality=87) return 'data:image/jpeg;base64,'+base64.b64encode(output.getvalue()).decode() def frame(t): capture.set(cv2.CAP_PROP_POS_MSEC,t*1000);ok,pixels=capture.read() if not ok:raise RuntimeError('Unable to decode selected video frame') im=Image.fromarray(cv2.cvtColor(pixels,cv2.COLOR_BGR2RGB));im.thumbnail((640,640)) raw=data(im);marked=im.copy();draw=ImageDraw.Draw(marked);b=binding['regionBox'];w,h=marked.size draw.rectangle((round(b['x']*w),round(b['y']*h),min(w-1,round((b['x']+b['w'])*w)),min(h-1,round((b['y']+b['h'])*h))),outline='#34d399',width=3) return {'image':raw,'annotated':data(marked),'mediaTime':t,'frameId':'replay-'+str(t),'sourceId':'recorded-test-camera'} result={'request':registration['request'],'binding':binding,'frames':[frame(t) for t in (0.1,1.1,2.1)],'observation':frame(3.1)} capture.release();(root/'.local/app-api-fixture.json').write_text(json.dumps(result,ensure_ascii=False),encoding='utf-8') print('Prepared recorded-video frames for real-model API acceptance')