hafizqaim commited on
Commit
c170c60
·
1 Parent(s): 7b68b02

fix: use /tmp directory for uploads

Browse files
Files changed (1) hide show
  1. app.py +6 -5
app.py CHANGED
@@ -21,8 +21,8 @@ except Exception as e:
21
  # A dictionary to store the paths of uploaded videos, using a session ID as the key
22
  video_sessions = {}
23
 
24
- # Create a directory to store temporary video uploads
25
- UPLOAD_DIR = "uploads"
26
  os.makedirs(UPLOAD_DIR, exist_ok=True)
27
 
28
 
@@ -42,7 +42,10 @@ async def read_root():
42
 
43
  def generate_webcam_frames():
44
  """Generates annotated frames from the webcam."""
45
- camera = cv2.VideoCapture(0) # 0 is the default webcam
 
 
 
46
  if not camera.isOpened():
47
  print("Error: Could not start webcam.")
48
  return
@@ -71,7 +74,6 @@ def generate_webcam_frames():
71
  @app.get("/video_feed/webcam")
72
  def video_feed_webcam():
73
  """Streams video from the webcam."""
74
- # Use StreamingResponse for generator functions
75
  return StreamingResponse(generate_webcam_frames(), media_type="multipart/x-mixed-replace; boundary=frame")
76
 
77
 
@@ -128,5 +130,4 @@ def generate_upload_frames(session_id: str):
128
  @app.get("/video_feed/upload")
129
  def video_feed_upload(session_id: str):
130
  """Streams video from an uploaded file based on the session ID."""
131
- # Use StreamingResponse for generator functions
132
  return StreamingResponse(generate_upload_frames(session_id), media_type="multipart/x-mixed-replace; boundary=frame")
 
21
  # A dictionary to store the paths of uploaded videos, using a session ID as the key
22
  video_sessions = {}
23
 
24
+ # *** FIX: Use the /tmp directory which is writeable on Hugging Face Spaces ***
25
+ UPLOAD_DIR = "/tmp/uploads"
26
  os.makedirs(UPLOAD_DIR, exist_ok=True)
27
 
28
 
 
42
 
43
  def generate_webcam_frames():
44
  """Generates annotated frames from the webcam."""
45
+ # Note: Webcam functionality will not work on Hugging Face Spaces
46
+ # as the server does not have a physical camera.
47
+ # This endpoint will work locally but fail on the server.
48
+ camera = cv2.VideoCapture(0)
49
  if not camera.isOpened():
50
  print("Error: Could not start webcam.")
51
  return
 
74
  @app.get("/video_feed/webcam")
75
  def video_feed_webcam():
76
  """Streams video from the webcam."""
 
77
  return StreamingResponse(generate_webcam_frames(), media_type="multipart/x-mixed-replace; boundary=frame")
78
 
79
 
 
130
  @app.get("/video_feed/upload")
131
  def video_feed_upload(session_id: str):
132
  """Streams video from an uploaded file based on the session ID."""
 
133
  return StreamingResponse(generate_upload_frames(session_id), media_type="multipart/x-mixed-replace; boundary=frame")