(self, video_path, frame_start, frame_pos, size)
| 30 | self.image_placeholder = '<image>\n' |
| 31 | |
| 32 | def _get_frame(self, video_path, frame_start, frame_pos, size): |
| 33 | if video_path.endswith('.jpg') or video_path.endswith('.png'): |
| 34 | image = Image.open(video_path).resize(size) |
| 35 | return image |
| 36 | if self.video_reader == 'cv2': |
| 37 | video_cap = cv2.VideoCapture(video_path) |
| 38 | num_frames = video_cap.get(cv2.CAP_PROP_FRAME_COUNT) |
| 39 | if frame_start + frame_pos >= num_frames or frame_start + frame_pos < 0: |
| 40 | frame_pos = 0 |
| 41 | trials = 0 |
| 42 | video_cap.set(cv2.CAP_PROP_POS_FRAMES, frame_start + frame_pos) |
| 43 | while trials < 5: |
| 44 | success, image = video_cap.read() |
| 45 | if success: |
| 46 | break |
| 47 | else: |
| 48 | time.sleep(0.1) |
| 49 | trials += 1 |
| 50 | if not success: |
| 51 | print(f"Failed to read video {video_path} at frame {frame_start + frame_pos}") |
| 52 | image = None |
| 53 | else: |
| 54 | image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) |
| 55 | image = Image.fromarray(image).resize(size) |
| 56 | video_cap.release() |
| 57 | return image |
| 58 | elif self.video_reader == 'decord': |
| 59 | try: |
| 60 | vr = VideoReader(video_path, ctx=cpu(0), num_threads=1) |
| 61 | num_frames = len(vr) |
| 62 | if frame_start+frame_pos >= num_frames: |
| 63 | frame_pos = 0 |
| 64 | frame_idx = [frame_start+frame_pos] |
| 65 | image = vr.get_batch(frame_idx).asnumpy()[0] |
| 66 | # https://github.com/dmlc/decord/issues/208 |
| 67 | vr.seek(0) |
| 68 | # convert image to rgb format |
| 69 | image = Image.fromarray(image).resize(size) |
| 70 | return image |
| 71 | except Exception as e: |
| 72 | print(f"Failed to read video {video_path} at frame {frame_start + frame_pos}") |
| 73 | return None |
| 74 | |
| 75 | def _process_gpt_response(self, gpt_response, task_description): |
| 76 | """ |
no outgoing calls
no test coverage detected