i do yolov8 segmentation.can I do inference batch of images? def extract(self, image,…
- User
i do yolov8 segmentation.can I do inference batch of images? def extract(self, image, image_filename, aligned_crop_path=None): results = self.model(image) original_height, original_width = image.shape[:2] image_area = original_height * original_width min_contour_area = image_area / 30 # 1/20 of the image area
codeextracted_images = [] for result in results: masks = result.masks if masks is None: continue for i, mask in enumerate(masks.data): mask = mask.cpu().numpy() mask = cv2.resize(mask, (original_width, original_height)) mask = (mask > 0.5).astype(np.uint8) * 255 contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) valid_contours = [contour for contour in contours if cv2.contourArea(contour) > min_contour_area] for contour in valid_contours: approx_contour = self.approximate_contour(contour) approx_contour = approx_contour[::-1] # Reverse the contour if len(approx_contour) != 4: continue class_name = self.class_names[result.boxes.cls[i].int().item()] if aligned_crop_path is not None: data = self.save_as_json(approx_contour, class_name, image_filename, original_height, original_width) base_filename = FileUtility.getFilename(image_filename) name, ext = os.path.splitext(base_filename) dst_image_filename = os.path.join(aligned_crop_path, f"{name}_{class_name}{ext}") json_filename = FileUtility.changeFileExt(dst_image_filename, 'json') with open(json_filename, 'w') as json_file: json.dump(data, json_file, indent=2) FileUtility.copyFile(image_filename, dst_image_filename) contour_dim = self.get_contour_dimension(approx_contour) dst_image = self.rectify_image(image, approx_contour, contour_dim) # cv2.imshow("pre",dst_image) # cv2.waitKey(0) dst_image ,box ,score = self.find_face(dst_image) if dst_image is not None: extracted_images.append((dst_image, class_name)) # cv2.imshow("view",dst_image) # cv2.waitKey(0) return extracted_images if extracted_images else None Assistant
- User
this is not batch inference yolo model!!!
Assistant
- User
your code is not modular!!!
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.