Spaces:
Running on T4
Running on T4
github-actions[bot] commited on
Commit ·
ae695bd
1
Parent(s): ffb9fd9
Automated Deploy: 5f5a7796b033a641e2d36ba6ab13a6325199a1a5
Browse files
baseball_batting/sam_measurement_worker.py
CHANGED
|
@@ -54,10 +54,10 @@ def process_predictions(job,video,capture,raw,options,model,*,progress=lambda *a
|
|
| 54 |
capture_times=physical,playback_times=playback)
|
| 55 |
progress('measurements',len(times),len(times))
|
| 56 |
group_swings,contexts=calculate_measurements(corrected,times,options,clock_known=known)
|
| 57 |
-
# Save the exact
|
| 58 |
np.savez_compressed(job/'measurement-curves-private.npz',**{
|
| 59 |
f"rep{c['rep']}_{key}":q for c in contexts for key,q in c['curves'].items()})
|
| 60 |
-
clips=export_group_videos(video,capture,corrected,raw['intrinsics'],
|
| 61 |
groups=[MeasurementGroup(key=key,label=label,swings=group_swings[key],annotated_video=clips[key]) for key,label in GROUPS.items()]
|
| 62 |
# Rules explanations are always present. The existing agent may add language
|
| 63 |
# using its provider, cache and shared daily budget; no images leave this path.
|
|
|
|
| 54 |
capture_times=physical,playback_times=playback)
|
| 55 |
progress('measurements',len(times),len(times))
|
| 56 |
group_swings,contexts=calculate_measurements(corrected,times,options,clock_known=known)
|
| 57 |
+
# Save the exact measurement curves without exposing them in HTTP responses.
|
| 58 |
np.savez_compressed(job/'measurement-curves-private.npz',**{
|
| 59 |
f"rep{c['rep']}_{key}":q for c in contexts for key,q in c['curves'].items()})
|
| 60 |
+
clips=export_group_videos(video,capture,corrected,raw['intrinsics'],job,progress)
|
| 61 |
groups=[MeasurementGroup(key=key,label=label,swings=group_swings[key],annotated_video=clips[key]) for key,label in GROUPS.items()]
|
| 62 |
# Rules explanations are always present. The existing agent may add language
|
| 63 |
# using its provider, cache and shared daily budget; no images leave this path.
|
baseball_batting/sam_videos.py
CHANGED
|
@@ -1,35 +1,21 @@
|
|
| 1 |
"""Four synchronized teaching overlays from exactly the corrected measurement points."""
|
| 2 |
from pathlib import Path
|
| 3 |
-
import textwrap
|
| 4 |
import numpy as np
|
| 5 |
import cv2
|
| 6 |
from .sam_batting_contract import GROUPS
|
| 7 |
-
from .sam_measurements import GROUP_KEYS,DEFINITIONS
|
| 8 |
from .sam_motion import BONES
|
| 9 |
from .ingest import iter_frames
|
| 10 |
from .geometry import project
|
| 11 |
from .swing_clips import _Writer,_source_time,_ticks,TIME_BASE,ClipError,Clip
|
| 12 |
from .storage import sha256_file
|
| 13 |
|
| 14 |
-
EXPLANATIONS={
|
| 15 |
-
'rotation':'Projected hip/shoulder lines; the displayed angles use the declared 3D horizontal plane.',
|
| 16 |
-
'timing':'Speed peaks show movement order. They do not establish swing onset or ball contact.',
|
| 17 |
-
'posture':'Head movement is relative to the hip midpoint. Torso lines describe lean, not foot pressure.',
|
| 18 |
-
'stance':'Relative stride compares the lead ankle with the rear ankle; it is not absolute ground travel.'}
|
| 19 |
FOCUS={'rotation':{5,6,9,10},'timing':{5,6,7,8,9,10,41,62},
|
| 20 |
'posture':{3,4,5,6,9,10},'stance':{9,10,11,12,13,14,15,16,17,18,19,20}}
|
| 21 |
|
| 22 |
|
| 23 |
-
def
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
cv2.putText(image,text.encode('ascii','replace').decode(),(int(x),int(y)),cv2.FONT_HERSHEY_SIMPLEX,size,color,thickness,cv2.LINE_AA)
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
def draw_group(image,xy,valid,group,frame,context,group_swings):
|
| 30 |
-
image=image.copy();h,w=image.shape[:2]
|
| 31 |
-
canvas_width=max(640,min(960,w))
|
| 32 |
-
scale=canvas_width/720;radius=max(2,round(w/220))
|
| 33 |
focus=FOCUS[group]
|
| 34 |
for a,b in BONES:
|
| 35 |
if not(valid[a] and valid[b]):continue
|
|
@@ -42,50 +28,10 @@ def draw_group(image,xy,valid,group,frame,context,group_swings):
|
|
| 42 |
if group=='posture' and valid[[3,4,9,10]].all():
|
| 43 |
head=np.mean(xy[[3,4]],axis=0);hip=np.mean(xy[[9,10]],axis=0)
|
| 44 |
cv2.line(image,tuple(np.rint(head).astype(int)),tuple(np.rint(hip).astype(int)),(60,195,255),max(1,radius-1),cv2.LINE_AA)
|
| 45 |
-
|
| 46 |
-
chars=max(35,round(canvas_width/(11*scale)))
|
| 47 |
-
lines=[]
|
| 48 |
-
for line in textwrap.wrap(EXPLANATIONS[group],chars):
|
| 49 |
-
lines.append((line,(185,203,215)))
|
| 50 |
-
swing=next((s for s in group_swings if context and s.rep==context['rep']),None)
|
| 51 |
-
for key in GROUP_KEYS[group]:
|
| 52 |
-
metric=next((m for m in swing.measurements if m.key==key),None) if swing else None
|
| 53 |
-
value=None;label,unit,_=DEFINITIONS[key]
|
| 54 |
-
if context:
|
| 55 |
-
q=context['curves'][key]
|
| 56 |
-
if np.isfinite(q[frame]):
|
| 57 |
-
value=float(q[frame])
|
| 58 |
-
if key=='hip_angular_velocity_deg_s':label='Current signed hip angular velocity'
|
| 59 |
-
elif key=='shoulder_angular_velocity_deg_s':label='Current signed shoulder angular velocity'
|
| 60 |
-
elif key=='hand_speed_relative_m_s':label='Current hand speed relative to hips'
|
| 61 |
-
elif metric and metric.value is not None:
|
| 62 |
-
# Event-only values are summaries, explicitly labelled as such.
|
| 63 |
-
value=metric.value;label+=' [summary]'
|
| 64 |
-
shown=f'{value:.1f} {unit}' if value is not None else 'unavailable'
|
| 65 |
-
for line in textwrap.wrap(label+': '+shown,chars):
|
| 66 |
-
lines.append((line,(225,235,242) if value is not None else (155,166,179)))
|
| 67 |
-
if context:
|
| 68 |
-
for name,k in context['peaks'].items():
|
| 69 |
-
if group=='timing' and k is not None and abs(frame-k)<=1:
|
| 70 |
-
lines.insert(0,(name.upper()+' SPEED PEAK',(100,230,170)))
|
| 71 |
-
if not valid[list(focus)].all():lines.insert(0,('Some required points are unreliable or missing',(80,185,255)))
|
| 72 |
-
maximum_lines=sum(max(1,len(textwrap.wrap(DEFINITIONS[key][0]+' [summary]: -99999.9 '+DEFINITIONS[key][1],chars))) for key in GROUP_KEYS[group])
|
| 73 |
-
maximum_lines+=len(textwrap.wrap(EXPLANATIONS[group],chars))+6
|
| 74 |
-
footer=padding*2+row_height*maximum_lines
|
| 75 |
-
# Fit the source picture before adding readable text, avoiding a later whole-
|
| 76 |
-
# frame shrink that would make portrait-video numbers too small to read.
|
| 77 |
-
image_scale=min(1,canvas_width/w,max(200,1280-footer)/h)
|
| 78 |
-
body_w=max(1,round(w*image_scale));body_h=max(1,round(h*image_scale))
|
| 79 |
-
image=cv2.resize(image,(body_w,body_h),interpolation=cv2.INTER_AREA)
|
| 80 |
-
output=np.zeros((body_h+footer,canvas_width,3),np.uint8)
|
| 81 |
-
offset=(canvas_width-body_w)//2;output[:body_h,offset:offset+body_w]=image
|
| 82 |
-
output[body_h:]=(25,36,48)
|
| 83 |
-
_text(output,GROUPS[group]+' | Frame '+str(frame)+' | ESTIMATE',padding,body_h+padding+row_height,.75*scale,(140,235,185),2)
|
| 84 |
-
for i,(line,color) in enumerate(lines):_text(output,line,padding,body_h+padding+(i+2)*row_height,.65*scale,color)
|
| 85 |
-
return output
|
| 86 |
|
| 87 |
|
| 88 |
-
def export_group_videos(video,capture,corrected,intrinsics,
|
| 89 |
job=Path(job);folder=job/'group-videos';folder.mkdir(exist_ok=True)
|
| 90 |
keys=list(GROUPS);clips={k:Clip(status='unavailable',reason_code='annotation_export_failed') for k in keys}
|
| 91 |
writers={};frames=capture['frames'];n=len(frames)
|
|
@@ -101,9 +47,8 @@ def export_group_videos(video,capture,corrected,intrinsics,contexts,groups,job,p
|
|
| 101 |
k=intrinsics[i]
|
| 102 |
xy=project(corrected['points_camera'][i],k) if np.isfinite(k).all() else np.full((70,2),np.nan)
|
| 103 |
valid=corrected['valid'][i]&np.isfinite(xy).all(axis=-1)
|
| 104 |
-
context=next((c for c in contexts if c['window']['start_frame']<=i<=c['window']['end_frame']),None)
|
| 105 |
for key in keys:
|
| 106 |
-
annotated=draw_group(image,xy,valid,key
|
| 107 |
if key not in writers:writers[key]=_Writer(temporary[key],annotated.shape)
|
| 108 |
writers[key].write(annotated,timeline[i],ticks)
|
| 109 |
if progress and (i%32==0 or i==n-1):progress('annotated_videos',i+1,n)
|
|
|
|
| 1 |
"""Four synchronized teaching overlays from exactly the corrected measurement points."""
|
| 2 |
from pathlib import Path
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import cv2
|
| 5 |
from .sam_batting_contract import GROUPS
|
|
|
|
| 6 |
from .sam_motion import BONES
|
| 7 |
from .ingest import iter_frames
|
| 8 |
from .geometry import project
|
| 9 |
from .swing_clips import _Writer,_source_time,_ticks,TIME_BASE,ClipError,Clip
|
| 10 |
from .storage import sha256_file
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
FOCUS={'rotation':{5,6,9,10},'timing':{5,6,7,8,9,10,41,62},
|
| 13 |
'posture':{3,4,5,6,9,10},'stance':{9,10,11,12,13,14,15,16,17,18,19,20}}
|
| 14 |
|
| 15 |
|
| 16 |
+
def draw_group(image,xy,valid,group):
|
| 17 |
+
image=image.copy();w=image.shape[1]
|
| 18 |
+
radius=max(2,round(w/220))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
focus=FOCUS[group]
|
| 20 |
for a,b in BONES:
|
| 21 |
if not(valid[a] and valid[b]):continue
|
|
|
|
| 28 |
if group=='posture' and valid[[3,4,9,10]].all():
|
| 29 |
head=np.mean(xy[[3,4]],axis=0);hip=np.mean(xy[[9,10]],axis=0)
|
| 30 |
cv2.line(image,tuple(np.rint(head).astype(int)),tuple(np.rint(hip).astype(int)),(60,195,255),max(1,radius-1),cv2.LINE_AA)
|
| 31 |
+
return image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
|
| 34 |
+
def export_group_videos(video,capture,corrected,intrinsics,job,progress=None):
|
| 35 |
job=Path(job);folder=job/'group-videos';folder.mkdir(exist_ok=True)
|
| 36 |
keys=list(GROUPS);clips={k:Clip(status='unavailable',reason_code='annotation_export_failed') for k in keys}
|
| 37 |
writers={};frames=capture['frames'];n=len(frames)
|
|
|
|
| 47 |
k=intrinsics[i]
|
| 48 |
xy=project(corrected['points_camera'][i],k) if np.isfinite(k).all() else np.full((70,2),np.nan)
|
| 49 |
valid=corrected['valid'][i]&np.isfinite(xy).all(axis=-1)
|
|
|
|
| 50 |
for key in keys:
|
| 51 |
+
annotated=draw_group(image,xy,valid,key)
|
| 52 |
if key not in writers:writers[key]=_Writer(temporary[key],annotated.shape)
|
| 53 |
writers[key].write(annotated,timeline[i],ticks)
|
| 54 |
if progress and (i%32==0 or i==n-1):progress('annotated_videos',i+1,n)
|
docs/SAM_BATTING_API.md
CHANGED
|
@@ -150,10 +150,10 @@ Do not hide an entire group merely because one of its values is unavailable.
|
|
| 150 |
Show the exact reason and its coaching explanation.
|
| 151 |
|
| 152 |
Each group has its own version of the complete recording, without audio, with
|
| 153 |
-
relevant skeleton markings
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
corrected coordinates. Videos resize to at most 1280 pixels on the longest side.
|
| 158 |
|
| 159 |
## Correction, geometry and conditional values
|
|
|
|
| 150 |
Show the exact reason and its coaching explanation.
|
| 151 |
|
| 152 |
Each group has its own version of the complete recording, without audio, with
|
| 153 |
+
relevant skeleton markings over the original picture. No bottom information
|
| 154 |
+
panel or measurement text is added; values remain in the JSON and page cards.
|
| 155 |
+
The videos use the same corrected 3D points, projected through the matching SAM
|
| 156 |
+
camera model. Native prediction coordinates never replace failed
|
| 157 |
corrected coordinates. Videos resize to at most 1280 pixels on the longest side.
|
| 158 |
|
| 159 |
## Correction, geometry and conditional values
|