Spaces:
Runtime error
Runtime error
File size: 6,911 Bytes
2b64964 ef4afed 2b64964 e6079a6 2b64964 |
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
from PIL import Image
import io
import pandas as pd
import numpy as np
from typing import Optional
from ultralytics import YOLO
from ultralytics.yolo.utils.plotting import Annotator, colors
# Initialize the models
model_sample_model = YOLO("https://github.com/NVT-Freelancer/LICENSES/raw/main/detect/playing-card/best_350.pt")
def get_image_from_bytes(binary_image: bytes) -> Image:
"""Convert image from bytes to PIL RGB format
Args:
binary_image (bytes): The binary representation of the image
Returns:
PIL.Image: The image in PIL RGB format
"""
input_image = Image.open(io.BytesIO(binary_image)).convert("RGB")
return input_image
def get_bytes_from_image(image: Image) -> bytes:
"""
Convert PIL image to Bytes
Args:
image (Image): A PIL image instance
Returns:
bytes : BytesIO object that contains the image in JPEG format with quality 85
"""
return_image = io.BytesIO()
image.save(return_image, format='JPEG', quality=85) # save the image in JPEG format with quality 85
return_image.seek(0) # set the pointer to the beginning of the file
return return_image
def transform_predict_to_df(results: list, labeles_dict: dict) -> pd.DataFrame:
"""
Transform predict from yolov8 (torch.Tensor) to pandas DataFrame.
Args:
results (list): A list containing the predict output from yolov8 in the form of a torch.Tensor.
labeles_dict (dict): A dictionary containing the labels names, where the keys are the class ids and the values are the label names.
Returns:
predict_bbox (pd.DataFrame): A DataFrame containing the bounding box coordinates, confidence scores and class labels.
"""
# Transform the Tensor to numpy array
predict_bbox = pd.DataFrame(results[0].to("cpu").numpy().boxes.xyxy, columns=['xmin', 'ymin', 'xmax', 'ymax'])
# Add the confidence of the prediction to the DataFrame
predict_bbox['confidence'] = results[0].to("cpu").numpy().boxes.conf
# Add the class of the prediction to the DataFrame
predict_bbox['class'] = (results[0].to("cpu").numpy().boxes.cls).astype(int)
# Replace the class number with the class name from the labeles_dict
predict_bbox['name'] = predict_bbox["class"].replace(labeles_dict)
return predict_bbox
def get_model_predict(model: YOLO, input_image: Image, save: bool = False, image_size: int = 1248, conf: float = 0.5,
augment: bool = False) -> pd.DataFrame:
"""
Get the predictions of a model on an input image.
Args:
model (YOLO): The trained YOLO model.
input_image (Image): The image on which the model will make predictions.
save (bool, optional): Whether to save the image with the predictions. Defaults to False.
image_size (int, optional): The size of the image the model will receive. Defaults to 1248.
conf (float, optional): The confidence threshold for the predictions. Defaults to 0.5.
augment (bool, optional): Whether to apply data augmentation on the input image. Defaults to False.
Returns:
pd.DataFrame: A DataFrame containing the predictions.
"""
# Make predictions
predictions = model.predict(
imgsz=image_size,
source=input_image,
conf=conf,
save=save,
augment=augment,
flipud=0.0,
fliplr=0.0,
mosaic=0.0,
)
# Transform predictions to pandas dataframe
predictions = transform_predict_to_df(predictions, model.model.names)
return predictions
def get_model_predict_origin(model: YOLO, input_image: Image, save: bool = False, image_size: int = 1248,
conf: float = 0.5,
augment: bool = False) -> pd.DataFrame:
"""
Get the predictions of a model on an input image.
Args:
model (YOLO): The trained YOLO model.
input_image (Image): The image on which the model will make predictions.
save (bool, optional): Whether to save the image with the predictions. Defaults to False.
image_size (int, optional): The size of the image the model will receive. Defaults to 1248.
conf (float, optional): The confidence threshold for the predictions. Defaults to 0.5.
augment (bool, optional): Whether to apply data augmentation on the input image. Defaults to False.
Returns:
pd.DataFrame: A DataFrame containing the predictions.
"""
# Make predictions
predictions = model.predict(
imgsz=image_size,
source=input_image,
conf=conf,
save=save,
augment=augment,
flipud=0.0,
fliplr=0.0,
mosaic=0.0,
)
# Transform predictions to pandas dataframe
return predictions
################################# BBOX Func #####################################
def add_bboxs_on_img(image: Image, predict: pd.DataFrame()) -> Image:
"""
add a bounding box on the image
Args:
image (Image): input image
predict (pd.DataFrame): predict from model
Returns:
Image: image whis bboxs
"""
# Create an annotator object
annotator = Annotator(np.array(image))
# sort predict by xmin value
predict = predict.sort_values(by=['xmin'], ascending=True)
# iterate over the rows of predict dataframe
for i, row in predict.iterrows():
# create the text to be displayed on image
text = f"{row['name']}: {int(row['confidence'] * 100)}%"
# get the bounding box coordinates
bbox = [row['xmin'], row['ymin'], row['xmax'], row['ymax']]
# add the bounding box and text on the image
annotator.box_label(bbox, text, color=colors(row['class'], True))
# convert the annotated image to PIL image
return Image.fromarray(annotator.result())
################################# Models #####################################
def detect_sample_model(input_image: Image) -> pd.DataFrame:
"""
Predict from sample_model.
Base on YoloV8
Args:
input_image (Image): The input image.
Returns:
pd.DataFrame: DataFrame containing the object location.
"""
predict = get_model_predict(
model=model_sample_model,
input_image=input_image,
save=False,
image_size=640,
augment=False,
conf=0.5,
)
return predict
def detect_sample_model_origin(input_image: Image) -> pd.DataFrame:
"""
Predict from sample_model.
Base on YoloV8
Args:
input_image (Image): The input image.
Returns:
pd.DataFrame: DataFrame containing the object location.
"""
predict = get_model_predict_origin(
model=model_sample_model,
input_image=input_image,
save=False,
image_size=640,
augment=False,
conf=0.5,
)
return predict
|