diff --git a/README.md b/README.md index 4fb3e03..0b6f175 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,73 @@ -# yolo +# Object Detection with Yolo + +*author: shiyu22* + + + +
+ + + +### Description + +**Object Detection** is a computer vision technique that locates and identifies people, items, or other objects in an image. Object detection has applications in many areas of computer vision, including image retrieval, image annotation, vehicle counting, object tracking, etc. + +This operator uses [PyTorch.yolov5](https://pytorch.org/hub/ultralytics_yolov5/) to detect the object. + + + +
+ + + +### Code Example + + + +Writing the pipeline in the simplified way + +```Python +import towhee + +towhee.glob('./test.png') \ + .image_decode() \ + .object_detection.yolov5() \ + .show() +``` + +results1 + + + +
+ + + +## Factory Constructor + +Create the operator via the following factory method + +***object_detection.yolov5()*** + + + +
+ + + +### Interface + +The operator takes an image as input. It first detects the objects appeared in the image, and gives the bounding box of each object. + +**Parameters:** + +​ **img**: numpy.ndarray + +​ Image data in ndarray format. + + + +**Return**: List[List[(int, int, int, int)], ...], List[str], List[float] + +The return value is a tuple of (boxes, classes, scores). The *boxes* is a list of bounding boxes. Each bounding box is represented by the top-left and the bottom right points, i.e. (x1, y1, x2, y2). The *classes* is a list of prediction labels. The *scores* is a list of the confidence scores. diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..95c54e3 --- /dev/null +++ b/__init__.py @@ -0,0 +1,4 @@ +from .yolov5 import Yolov5 + +def yolo(): + return Yolov5() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..e27b04f --- /dev/null +++ b/requirements.txt @@ -0,0 +1,5 @@ +matplotlib>=3.2.2 +opencv-python>=4.1.2 +torch>=1.7.0 +torchvision>=0.8.1 +seaborn diff --git a/test.png b/test.png new file mode 100755 index 0000000..3124577 Binary files /dev/null and b/test.png differ diff --git a/yolov5.py b/yolov5.py new file mode 100644 index 0000000..f35679b --- /dev/null +++ b/yolov5.py @@ -0,0 +1,21 @@ +import torch +import numpy + +from towhee import register +from towhee.operator import NNOperator + +@register(output_schema=['boxes', 'classes', 'scores']) +class Yolov5(NNOperator): + def __init__(self, model_name: str ='yolov5s'): + super().__init__() + self._model = torch.hub.load("ultralytics/yolov5", model_name, pretrained=True) + + def __call__(self, img: numpy.ndarray): + # Get object detection results with YOLOv5 model + results = self._model(img) + + boxes = [re[0:4] for re in results.xyxy[0]] + boxes = [list(map(int, box)) for box in boxes] + classes = list(results.pandas().xyxy[0].name) + scores = list(results.pandas().xyxy[0].confidence) + return [(b, c, s) for b, c, s in zip(boxes, classes, scores)]