kristyc commited on
Commit
ff3b552
·
1 Parent(s): 33723c9

Add logging

Browse files
Files changed (2) hide show
  1. app.py +17 -10
  2. log_utils.py +9 -0
app.py CHANGED
@@ -4,6 +4,9 @@ import mediapipe as mp
4
  import numpy as np
5
  import tempfile
6
  import mediapy as media
 
 
 
7
 
8
  mp_hands = mp.solutions.hands
9
  mp_hands_connections = mp.solutions.hands_connections
@@ -20,24 +23,27 @@ connections = {
20
  'HAND_PINKY_FINGER_CONNECTIONS': mp_hands_connections.HAND_PINKY_FINGER_CONNECTIONS,
21
  }
22
 
23
- def process_image(img, selected_connection, draw_background):
24
- print(img)
25
  results = hands.process(img)
26
  output_img = img if draw_background else np.zeros_like(img)
27
  if results.multi_hand_landmarks:
28
  for hand_landmarks in results.multi_hand_landmarks:
29
- mp_draw.draw_landmarks(output_img, hand_landmarks, connections[selected_connection])
30
  return output_img
31
 
 
 
 
32
 
33
- def process_video(video_path, selected_connection, draw_background):
 
34
  with tempfile.NamedTemporaryFile() as f:
35
  out_path = f"{f.name}.{video_path.split('.')[-1]}"
36
  with media.VideoReader(video_path) as r:
37
  with media.VideoWriter(
38
  out_path, shape=r.shape, fps=r.fps, bps=r.bps) as w:
39
  for image in r:
40
- w.add_image(process_image(image, selected_connection, draw_background))
41
  return out_path
42
 
43
 
@@ -53,7 +59,7 @@ with demo:
53
  with gr.Column():
54
  draw_background = gr.Checkbox(value=True, label="Draw background?")
55
  connection_keys = list(connections.keys())
56
- selected_connection = gr.Dropdown(
57
  label="Select connections to draw",
58
  choices=connection_keys,
59
  value=connection_keys[0],
@@ -77,9 +83,10 @@ with demo:
77
  processed_image = gr.Image()
78
 
79
  gr.Markdown('<img id="visitor-badge" alt="visitor badge" src="https://visitor-badge.glitch.me/badge?page_id=kristyc.mediapipe-hands" />')
80
- submit_uploaded_image.click(fn=process_image, inputs=[uploaded_image, selected_connection, draw_background], outputs=[processed_image])
81
- submit_camera_picture.click(fn=process_image, inputs=[camera_picture, selected_connection, draw_background], outputs=[processed_image])
82
- submit_recorded_video.click(fn=process_video, inputs=[recorded_video, selected_connection, draw_background], outputs=[processed_video])
83
- submit_uploaded_video.click(fn=process_video, inputs=[recorded_video, selected_connection, draw_background], outputs=[processed_video])
 
84
 
85
  demo.launch()
 
4
  import numpy as np
5
  import tempfile
6
  import mediapy as media
7
+ import log_utils
8
+
9
+ logger = log_utils.get_logger()
10
 
11
  mp_hands = mp.solutions.hands
12
  mp_hands_connections = mp.solutions.hands_connections
 
23
  'HAND_PINKY_FINGER_CONNECTIONS': mp_hands_connections.HAND_PINKY_FINGER_CONNECTIONS,
24
  }
25
 
26
+ def draw_landmarks(img, selected_connections, draw_background):
 
27
  results = hands.process(img)
28
  output_img = img if draw_background else np.zeros_like(img)
29
  if results.multi_hand_landmarks:
30
  for hand_landmarks in results.multi_hand_landmarks:
31
+ mp_draw.draw_landmarks(output_img, hand_landmarks, connections[selected_connections])
32
  return output_img
33
 
34
+ def process_image(img, selected_connections, draw_background):
35
+ logger.info(f"Processing image with connections: {selected_connections}, draw background: {draw_background}")
36
+ return draw_landmarks(img, selected_connections, draw_background)
37
 
38
+ def process_video(video_path, selected_connections, draw_background):
39
+ logger.info(f"Processing video with connections: {selected_connections}, draw background: {draw_background}")
40
  with tempfile.NamedTemporaryFile() as f:
41
  out_path = f"{f.name}.{video_path.split('.')[-1]}"
42
  with media.VideoReader(video_path) as r:
43
  with media.VideoWriter(
44
  out_path, shape=r.shape, fps=r.fps, bps=r.bps) as w:
45
  for image in r:
46
+ w.add_image(draw_landmarks(image, selected_connections, draw_background))
47
  return out_path
48
 
49
 
 
59
  with gr.Column():
60
  draw_background = gr.Checkbox(value=True, label="Draw background?")
61
  connection_keys = list(connections.keys())
62
+ selected_connections = gr.Dropdown(
63
  label="Select connections to draw",
64
  choices=connection_keys,
65
  value=connection_keys[0],
 
83
  processed_image = gr.Image()
84
 
85
  gr.Markdown('<img id="visitor-badge" alt="visitor badge" src="https://visitor-badge.glitch.me/badge?page_id=kristyc.mediapipe-hands" />')
86
+ setting_inputs = [selected_connections, draw_background]
87
+ submit_uploaded_image.click(fn=process_image, inputs=[uploaded_image, *setting_inputs], outputs=[processed_image])
88
+ submit_camera_picture.click(fn=process_image, inputs=[camera_picture, *setting_inputs], outputs=[processed_image])
89
+ submit_recorded_video.click(fn=process_video, inputs=[recorded_video, *setting_inputs], outputs=[processed_video])
90
+ submit_uploaded_video.click(fn=process_video, inputs=[recorded_video, *setting_inputs], outputs=[processed_video])
91
 
92
  demo.launch()
log_utils.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ import logging
2
+
3
+ logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
4
+
5
+ def get_logger(name=None, level=logging.INFO):
6
+ """Return a logger"""
7
+ logger = logging.getLogger(name)
8
+ logger.setLevel(level)
9
+ return logger