-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from 5sControl/dev
Dev
- Loading branch information
Showing
32 changed files
with
4,810 additions
and
134 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import requests | ||
import numpy as np | ||
from logging import Logger | ||
from PIL import Image | ||
import io | ||
|
||
|
||
PORT = 5000 | ||
|
||
|
||
class ModelPredictionsReceiver: | ||
def __init__(self, server_url: str, logger: Logger) -> None: | ||
self._server_url = server_url | ||
self._logger = logger | ||
|
||
@staticmethod | ||
def _convert_image2bytes(image: np.array, format='PNG') -> io.BytesIO: | ||
pil_image = Image.fromarray(image) | ||
img_byte_arr = io.BytesIO() | ||
pil_image.save(img_byte_arr, format=format) | ||
img_byte_arr.seek(0) | ||
return img_byte_arr | ||
|
||
def predict_human(self, img: np.array): | ||
try: | ||
response = requests.post( | ||
f"{self._server_url}:{PORT}/predict_human", | ||
files={ | ||
"image": ("image", self._convert_image2bytes(img), "image/png") | ||
} | ||
) | ||
response.raise_for_status() | ||
return np.array(response.json().get("coordinates")) | ||
except Exception as exc: | ||
self._logger.critical("Cannot send request to model server. Error - {}".format(exc)) | ||
|
||
def predict_bottles(self, img: np.array): | ||
try: | ||
response = requests.post( | ||
f"{self._server_url}:{PORT}/predict_bottles", | ||
files={ | ||
"image": ("image", self._convert_image2bytes(img), "image/png") | ||
} | ||
) | ||
response.raise_for_status() | ||
return np.array(response.json().get("coordinates")) | ||
except Exception as exc: | ||
self._logger.critical("Cannot send request to model server. Error - {}".format(exc)) | ||
|
||
def predict_boxes(self, img: np.array): | ||
try: | ||
response = requests.post( | ||
f"{self._server_url}:{PORT}/predict_boxes", | ||
files={ | ||
"image": ("image", self._convert_image2bytes(img), "image/png") | ||
} | ||
) | ||
response.raise_for_status() | ||
return np.array(response.json().get("coordinates")) | ||
except Exception as exc: | ||
self._logger.critical("Cannot send request to model server. Error - {}".format(exc)) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from .HTTPLIB2Capture import HTTPLIB2Capture | ||
from .ModelPredictionsReceiver import ModelPredictionsReceiver |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import torch | ||
from yolor.model import get_model | ||
import numpy as np | ||
from yolor.utils.datasets import letterbox | ||
from yolor.utils.general import non_max_suppression, scale_coords | ||
|
||
|
||
class YOLORObjectDetectionModel: | ||
def __init__(self, model_path: str, config_path: str, conf_thresh, iou_thresh, classes) -> None: | ||
self.model, self.device = get_model(model_path, config_path) | ||
self.conf_thresh = conf_thresh | ||
self.iou_thresh = iou_thresh | ||
self.classes = classes | ||
|
||
def __preprocess_image__(self, img: np.array) -> np.array: | ||
self.img_shape = img.shape | ||
img = letterbox(img.copy(), new_shape=1280, auto_size=64)[0] | ||
img = img[:, :, ::-1].transpose(2, 0, 1) | ||
img = np.ascontiguousarray(img) | ||
img = torch.from_numpy(img).to(self.device) | ||
img = img.float() | ||
img /= 255.0 | ||
img = img.unsqueeze(0) | ||
return img | ||
|
||
@torch.no_grad() | ||
def __call__(self, img: np.array) -> list: | ||
img = self.__preprocess_image__(img) | ||
pred = self.model(img, augment=False)[0] | ||
pred = non_max_suppression( | ||
pred, 0.45, 0.5, classes=self.classes, agnostic=False)[0] | ||
pred[:, :4] = scale_coords( | ||
img.shape[2:], pred[:, :4], self.img_shape).round() | ||
return pred[:, :5] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .model import get_model |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import torch | ||
from .utils.torch_utils import select_device | ||
from .models.models import Darknet | ||
|
||
|
||
def get_model(weights, cfg): | ||
imgsz = 1280 | ||
device = select_device('cpu') | ||
model = Darknet(cfg, imgsz) | ||
model.load_state_dict(torch.load(weights, map_location=device)['model']) | ||
model.to(device).eval() | ||
return model, device |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
Oops, something went wrong.