This repository was archived by the owner on Apr 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathneuralnetwork.py
More file actions
33 lines (31 loc) 路 1.3 KB
/
Copy pathneuralnetwork.py
File metadata and controls
33 lines (31 loc) 路 1.3 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
from imageai.Detection import ObjectDetection
from PIL import Image
import cv2
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '1' # or any {'0', '1', '2'}
import tensorflow as tf
import numpy as np
import io
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
class NeuralNetwork(object):
def __init__(self):
self.model = ObjectDetection()
self.model.setModelTypeAsYOLOv3()
self.model.setModelPath(os.path.join(os.getcwd(), "yolo.h5"))
self.model.loadModel(detection_speed="faster")
def predict(self, raw):
print("Predicting...")
image = np.array(Image.open(io.BytesIO(raw)))
detections = self.model.detectObjectsFromImage(input_image=image, input_type="array", output_type="array", minimum_percentage_probability=30)
return detections[1]
gpus = tf.config.experimental.list_physical_devices('GPU')
if gpus:
try:
# Currently, memory growth needs to be the same across GPUs
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
logical_gpus = tf.config.experimental.list_logical_devices('GPU')
print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPUs")
except RuntimeError as e:
# Memory growth must be set before GPUs have been initialized
print(e)