MarioPrzBasto commited on
Commit
f1ea500
·
verified ·
1 Parent(s): d7f4589

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +25 -10
main.py CHANGED
@@ -11,7 +11,7 @@ import requests
11
 
12
  import mimetypes
13
  import tempfile
14
- from moviepy.editor import VideoFileClip
15
 
16
 
17
  from PIL import Image
@@ -73,17 +73,30 @@ def mobilenet_similarity(img1, img2):
73
  print("Erro ao calcular similaridade com MobileNet")
74
  return 0
75
 
76
- def load_image(source, frame_time=1):
77
  Image.MAX_IMAGE_PIXELS = None
78
 
79
  def extract_frame_from_video(video_path_or_url, time_sec):
80
- clip = VideoFileClip(video_path_or_url)
81
- frame = clip.get_frame(time_sec) # frame RGB numpy array
82
- clip.reader.close()
83
- if clip.audio:
84
- clip.audio.reader.close_proc()
85
- frame_gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
86
- return frame_gray
 
 
 
 
 
 
 
 
 
 
 
 
 
87
 
88
  if source.startswith('http'):
89
  mime_type, _ = mimetypes.guess_type(source)
@@ -99,13 +112,15 @@ def load_image(source, frame_time=1):
99
  else:
100
  try:
101
  img_bytes = base64.b64decode(source)
 
 
102
  # Tenta abrir como imagem
103
  img = Image.open(BytesIO(img_bytes))
104
  img = np.array(img)
105
  img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
106
  return img
107
  except Exception:
108
- # Se falhar, assume que é vídeo em base64 e extrai frame
109
  with tempfile.NamedTemporaryFile(suffix='.mp4', delete=False) as temp_video:
110
  temp_video.write(base64.b64decode(source))
111
  temp_video_path = temp_video.name
 
11
 
12
  import mimetypes
13
  import tempfile
14
+ import subprocess
15
 
16
 
17
  from PIL import Image
 
73
  print("Erro ao calcular similaridade com MobileNet")
74
  return 0
75
 
76
+ def load_image(source, ffmpeg_path='ffmpeg', frame_time=1):
77
  Image.MAX_IMAGE_PIXELS = None
78
 
79
  def extract_frame_from_video(video_path_or_url, time_sec):
80
+ with tempfile.NamedTemporaryFile(suffix='.jpg', delete=False) as temp_frame:
81
+ frame_path = temp_frame.name
82
+
83
+ command = [
84
+ ffmpeg_path,
85
+ "-ss", str(time_sec),
86
+ "-i", video_path_or_url,
87
+ "-frames:v", "1",
88
+ "-q:v", "2",
89
+ "-y",
90
+ frame_path
91
+ ]
92
+ subprocess.run(command, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
93
+
94
+ if not os.path.exists(frame_path):
95
+ raise ValueError("Failed to extract frame from video.")
96
+
97
+ frame = cv2.imread(frame_path, cv2.IMREAD_GRAYSCALE)
98
+ os.remove(frame_path)
99
+ return frame
100
 
101
  if source.startswith('http'):
102
  mime_type, _ = mimetypes.guess_type(source)
 
112
  else:
113
  try:
114
  img_bytes = base64.b64decode(source)
115
+ mime_type = mimetypes.guess_type("data")[0] # fallback
116
+
117
  # Tenta abrir como imagem
118
  img = Image.open(BytesIO(img_bytes))
119
  img = np.array(img)
120
  img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
121
  return img
122
  except Exception:
123
+ # Se falhar, assumimos que é vídeo em base64 e extraímos frame
124
  with tempfile.NamedTemporaryFile(suffix='.mp4', delete=False) as temp_video:
125
  temp_video.write(base64.b64decode(source))
126
  temp_video_path = temp_video.name