-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcv_module.py
More file actions
41 lines (33 loc) · 1.11 KB
/
cv_module.py
File metadata and controls
41 lines (33 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"""
Computer Vision Module for the Enhanced AGI Pipeline.
"""
import torch
from loguru import logger
from PIL import Image
from ultralytics import YOLO
class CVModule:
"""
A module for Computer Vision tasks using YOLOv8.
"""
def __init__(self):
"""
Initializes the YOLOv8 model.
"""
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
self.model = YOLO("yolov8n.pt").to(self.device)
logger.info("CV model loaded successfully.")
def detect_objects(self, image: Image.Image) -> str:
"""Detects objects in the provided image.
Args:
image (Image.Image): The input image.
Returns:
str: JSON string containing detection results.
Raises:
ValueError: If the image is None.
"""
if image is None:
raise ValueError("Image cannot be None.")
logger.debug("Detecting objects in the image.")
results = self.model(image)
# In YOLOv8, results is a list. Each result object has a to_json method.
return results[0].to_json()