datasciencedojo commited on
Commit
a98a90d
·
verified ·
1 Parent(s): 292c4d1

Fix: AttributeError: 'Video' object has no attribute 'stream'

Browse files
Files changed (1) hide show
  1. app.py +17 -10
app.py CHANGED
@@ -6,8 +6,9 @@ mp_drawing = mp.solutions.drawing_utils
6
  mp_drawing_styles = mp.solutions.drawing_styles
7
  mp_hands = mp.solutions.hands
8
 
9
- def fun(img):
10
- print(type(img))
 
11
  with mp_hands.Hands(
12
  model_complexity=0,
13
  min_detection_confidence=0.5,
@@ -17,7 +18,7 @@ def fun(img):
17
  image = cv2.flip(img[:, :, ::-1], 1)
18
  # Convert the BGR image to RGB before processing.
19
  results = hands.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
20
- image.flags.writeable = True
21
  if results.multi_hand_landmarks:
22
  for hand_landmarks in results.multi_hand_landmarks:
23
  mp_drawing.draw_landmarks(
@@ -27,21 +28,27 @@ def fun(img):
27
  mp_drawing_styles.get_default_hand_landmarks_style(),
28
  mp_drawing_styles.get_default_hand_connections_style()
29
  )
30
-
31
  return cv2.flip(image[:, :, ::-1], 1)
32
 
33
  with gr.Blocks(title="Realtime Keypoint Detection | Data Science Dojo", css="footer {display:none !important} .output-markdown{display:none !important}") as demo:
34
  with gr.Row():
35
  with gr.Column():
36
- webcam_input = gr.Video(label="Webcam Input", format="mp4") # Webcam input
37
 
38
  with gr.Column():
39
- output = gr.Image(label="Output Image")
 
 
 
 
 
 
 
40
 
41
- webcam_input.stream(
42
- fn=fun,
43
- inputs=webcam_input,
44
- outputs=output
45
  )
46
 
47
  demo.launch(debug=True)
 
6
  mp_drawing_styles = mp.solutions.drawing_styles
7
  mp_hands = mp.solutions.hands
8
 
9
+ def process_frame(img):
10
+ if img is None:
11
+ return None
12
  with mp_hands.Hands(
13
  model_complexity=0,
14
  min_detection_confidence=0.5,
 
18
  image = cv2.flip(img[:, :, ::-1], 1)
19
  # Convert the BGR image to RGB before processing.
20
  results = hands.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
21
+ img.flags.writeable = True
22
  if results.multi_hand_landmarks:
23
  for hand_landmarks in results.multi_hand_landmarks:
24
  mp_drawing.draw_landmarks(
 
28
  mp_drawing_styles.get_default_hand_landmarks_style(),
29
  mp_drawing_styles.get_default_hand_connections_style()
30
  )
 
31
  return cv2.flip(image[:, :, ::-1], 1)
32
 
33
  with gr.Blocks(title="Realtime Keypoint Detection | Data Science Dojo", css="footer {display:none !important} .output-markdown{display:none !important}") as demo:
34
  with gr.Row():
35
  with gr.Column():
36
+ video_input = gr.Video(label="Webcam Input", format="mp4") # Video input for real-time processing
37
 
38
  with gr.Column():
39
+ output_image = gr.Image(label="Output Image") # Processed image output
40
+
41
+ def update_frame(video):
42
+ frame = cv2.VideoCapture(video).read()[1] # Read the current frame
43
+ if frame is not None:
44
+ processed_frame = process_frame(frame)
45
+ return processed_frame
46
+ return None
47
 
48
+ video_input.change(
49
+ update_frame,
50
+ inputs=[video_input],
51
+ outputs=[output_image]
52
  )
53
 
54
  demo.launch(debug=True)