I have trained a model to detect faces. Now for an assignment I am trying to detect faces without a mask as an anomaly. I trained my model in the yolov5 google collab with a public dataset from Roboflow.
I tried loading the model into my python notebook using opencv (cv2.dnn.readNetFromONNX()), but I was getting an error that opencv has no attribute 'dnn'. For this reason I swapped to the following code, which correctly imports the model as far as I can tell, but won't allow me to use model.predict. I suspect it's because of the model type, but I was not able to find any information online about this exact issue.
```from features import quantify_image
import argparse
import pickle
import cv2
import onnx
ap = argparse.ArgumentParser()
ap.add_argument("-m", "--model", required=True,
help="path to trained anomaly detection model")
ap.add_argument("-i", "--image", required=True,
help="path to input image")
args = vars(ap.parse_args())
#Laad het anomaly detection model
print("[INFO] loading anomaly detection model...")
model_dir = "./mnist"
# model = model_dir+"/gezichten.onnx" #does not work (sees the model as a string)
# net = cv2.dnn.readNetFromONNX('gezichten.onnx') #does not work
model = onnx.load('gezichten.onnx')
onnx.checker.check_model(model)
print(type(model))
#Laad de input afbeelding en converteer deze naar de HSV color space
#En quantify de image opnieuw
image = cv2.imread(args["image"])
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
features = quantify_image(hsv, bins=(3,3,3))
preds = model.predict([features])[0]
label = "anomaly" if preds == -1 else "normal"
color = (0,0,255) if preds == -1 else (0,255,0)
#Weergeef de label tekst op de image
cv2.putText(image, label, (10,25), cv2.FONT_HERSHEY_SIMPLEX,
0.7, color, 2)
#Weergeef de afbeelding met label
cv2.imshow("Output", image)
cv2.waitKey(0)
This code uses a function called 'quantify image', this is the following:
from imutils import paths
import numpy as np
import cv2
def quantify_image(image, bins=(4,6,3)):
#Compute een 3D kleurenhistogram over de afbeelding en normaliseer deze
hist = cv2.calcHist([image], [0,1,2], None, bins,
[0,180,0,256,0,256])
hist = cv2.normalize(hist, hist).flatten()
#return de histogram
return hist
def load_dataset(datasetPath, bins):
#Pak de paths naar alle afbeeldingen in onze dataset directory
#Dan initialiseren we onze lists met afbeeldingen
imagePaths = list(paths.list_images(datasetPath))
data = []
#Loop over de paths van de afbeeldingen
for imagePath in imagePaths:
#Laad de afbeeldinge en converteer deze naar de HSV color space
image = cv2.imread(imagePath)
image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
#Quantify de afbeeldingen en update de data list
features = quantify_image(image, bins)
data.append(features)
#return
return np.array(data)```
The error I exactly get is:
[INFO] loading anomaly detection model...
Traceback (most recent call last):
File "test_anomaly_detector2.py", line 49, in <module>
preds = model.predict([features])[0]
AttributeError: predict
This error occurs when I try to run the following command in terminal:
python test_anomaly_detector2.py --model gezichten.onnx --image 429yrpybeotg.jpeg
I use MacOS.
OpenCV version: 4.6.0
ONNX version: 1.13.0
ONNXRuntime version: 1.13.1