MarioPrzBasto commited on
Commit
d1543f6
·
verified ·
1 Parent(s): 3f66b64

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +53 -6
main.py CHANGED
@@ -8,6 +8,12 @@ import cv2
8
  import numpy as np
9
  import base64
10
  import requests
 
 
 
 
 
 
11
  from PIL import Image
12
  from io import BytesIO
13
  from tensorflow.keras.applications import MobileNetV2
@@ -70,17 +76,58 @@ def mobilenet_similarity(img1, img2):
70
  def load_image(source):
71
  Image.MAX_IMAGE_PIXELS = None
72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
  if source.startswith('http'):
 
 
 
 
 
74
  response = requests.get(source)
75
  img = np.asarray(bytearray(response.content), dtype=np.uint8)
76
  img = cv2.imdecode(img, cv2.IMREAD_GRAYSCALE)
77
- else:
78
- img = base64.b64decode(source)
79
- img = Image.open(BytesIO(img))
80
- img = np.array(img)
81
- img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
82
 
83
- return img
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
 
85
  app = FastAPI()
86
 
 
8
  import numpy as np
9
  import base64
10
  import requests
11
+
12
+ import mimetypes
13
+ import tempfile
14
+ import subprocess
15
+
16
+
17
  from PIL import Image
18
  from io import BytesIO
19
  from tensorflow.keras.applications import MobileNetV2
 
76
  def load_image(source):
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)
103
+ if mime_type and mime_type.startswith('video'):
104
+ return extract_frame_from_video(source, frame_time)
105
+
106
+ # Assume imagem
107
  response = requests.get(source)
108
  img = np.asarray(bytearray(response.content), dtype=np.uint8)
109
  img = cv2.imdecode(img, cv2.IMREAD_GRAYSCALE)
110
+ return img
 
 
 
 
111
 
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
127
+
128
+ frame = extract_frame_from_video(temp_video_path, frame_time)
129
+ os.remove(temp_video_path)
130
+ return frame
131
 
132
  app = FastAPI()
133