Frame and keras model - python

I have a model which predicting the steering angle of a car from a picture which I like to implement in a android project by getting the frames from the camera.
in my Python code I'm using a h5 file instead of the tflite file in which the picture is converted to numpy and get processed using Cv2 lib.
Python Code:
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import load_model
from PIL import Image
import cv2
def img_preprocess(img):
img = img[60:135,:,:]
img = cv2.cvtColor(img, cv2.COLOR_RGB2YUV)
img = cv2.GaussianBlur(img, (3, 3), 0)
img = cv2.resize(img, (200, 66))
img = img/255
return img
if __name__ == '__main__':
model = load_model('model.h5')
image1 = Image.open("Gta2.png")
image1 = np.asarray(image1)
image1 = img_preprocess(image1)
image1 = np.array([image1])
steering_angle = float(model.predict(image1))
if(steering_angle > 0):
print('turn right')
print('turn wheel : {}'.format(steering_angle))
else:
print('turn left')
print('turn wheel : {}'.format(steering_angle))
I've been imported the model.tflite to my project assets and know I need to process the CameraBridgeViewBase.CvCameraViewFrame object from the camera to fit to my model input.
the model input and output.
so my questions are:
How process the CvCameraViewFrame Object as in the python code in the 'img_preprocess' function?
How to reach the input specs?
my android code:

You can first convert your Mat frame to Bitmap, such as the solution in this question.
And then use TFLite Task library ImageClassifier to run the inference. Here is an example of how to use ImageClassifier, which comes from the TFLite Image Classification reference app.
Alternatively, you can write your own code to process the image and run inference using Interpreter. See the example here.

Related

AttributeError: module 'cv2' has no attribute 'face' OpenCV 4.7.0

After referring to some Stack Overflow answers I did pip install opencv-contrib-python, still I am getting those errors.
I am using OpenCV 4.7.0.
This is for a facial recognition project tutorial that I am following.
import cv2
import numpy as np
from PIL import Image
import os
# Path for face image database
path = 'dataset'
recognizer = cv2.face.LBPHFaceRecognizer_create()
detector = cv2.CascadeClassifier("haarcascade_frontalface_default.xml");
# function to get the images and label data
def getImagesAndLabels(path):
imagePaths = [os.path.join(path,f) for f in os.listdir(path)]
faceSamples=[]
ids = []
for imagePath in imagePaths:
PIL_img = Image.open(imagePath).convert('L') # grayscale
img_numpy = np.array(PIL_img,'uint8')
id = int(os.path.split(imagePath)[-1].split(".")[1])
faces = detector.detectMultiScale(img_numpy)
for (x,y,w,h) in faces:
faceSamples.append(img_numpy[y:y+h,x:x+w])
ids.append(id)
return faceSamples,ids
print ("\n [INFO] Training faces. It will take a few seconds. Wait ...")
faces,ids = getImagesAndLabels(path)
recognizer.train(faces, np.array(ids))
# Save the model into trainer/trainer.yml
recognizer.write('trainer/trainer.yml')
# Print the numer of faces trained and end program
print("\n [INFO] {0} faces trained. Exiting Program".format(len(np.unique(ids))))
I keep getting this error:
File "/Users/sashuponnaganti/workspace/Facial Recognition Project/face_trainer.py", line 7, in <module>
recognizer = cv2.face.LBPHFaceRecognizer_create()
AttributeError: module 'cv2' has no attribute 'face'
Any ideas how to fix this?
I already tried doing pip install opencv-contrib-python and I had already installed it so it made no difference.
This question has been solved. I had installed all my opencv packages in a conda environment but was running it with the wrong interpreter.

OpenCV: Error in loading Net from onnx file

I'm trying to load with cv.dnn.readNetFromONNX a pre-trained torch model (U2Net to be precise) saved as onnx.
But I'm receiving the error:
error: OpenCV(4.1.2) /io/opencv/modules/dnn/include/opencv2/dnn/dnn.inl.hpp:349:
error (-204:Requested object was not found) Required argument "starts" not found
into dictionary in function 'get'
This is the code to reproduce the error with Google Colab:
### get U2Net implementation ###
%cd /content
!git clone https://github.com/shreyas-bk/U-2-Net
### download pre-trained model ###
!gdown --id 1ao1ovG1Qtx4b7EoskHXmi2E9rp5CHLcZ -O /content/U-2-Net/u2net.pth
###
%cd /content/U-2-Net
### imports ###
from google.colab import files
from model import U2NET
import torch
import os
### create U2Net model from state
model_dir = '/content/U-2-Net/u2net.pth'
net = U2NET(3, 1)
net.load_state_dict(torch.load(model_dir, map_location='cpu'))
net.eval()
### pass to it a dummy input and save to onnx ###
img = torch.randn(1, 3, 320, 320, requires_grad=False)
img = img.to(torch.device('cpu'))
output_dir = os.path.join('/content/u2net.onnx')
torch.onnx.export(net, img, output_dir, opset_version=11, verbose=True)
### load the model in OpenCV ###
import cv2 as cv
net = cv.dnn.readNetFromONNX('/content/u2net.onnx')
[ OpenCV => 4.1.2, Platform => Google Colab, Torch => 1.11.0+cu113]
As #berak suggestet, the issue was related to the OpenCV version (was 4.1.2). Updating to 4.5.5 solved the issue.

I am trying to make a video chat-app using imagezmq and opencv

Here is the code:
Server:
import cv2
import numpy as np
import imagezmq
image_hub = imagezmq.ImageHub()
while True: # show streamed images until Ctrl-C
win_name = "Feed"
image = image_hub.recv_image()
img = cv2.imdecode(image, 1)
cv2.imshow(win_name, img)
cv2.waitKey(1)
image_hub.send_reply(b'OK')
Client:
import socket
import time
from imutils.video import VideoStream
import imagezmq
import cv2
import numpy as np
sender = imagezmq.ImageSender(connect_to='tcp://192.168.0.12:5555')
vs = VideoStream(src=0).start()
time.sleep(2.0)
while True: # send images as stream until Ctrl-C
frame = vs.read()
img_arr = np.array(bytearray(frame))
sender.send_image("Img",img_arr)
And i get the error:
img = cv2.imdecode(image, 1)
TypeError: Expected Ptr<cv::UMat> for argument 'buf'
on the server
I have checked online but could not find a suitable answer
Note: i am not that experienced in this.
according to imagezmq's documentation, one does not use any imdecode, and one does not use any bytearray() calls.
you just send numpy arrays back and forth.
further, I would advise against using imutils, in particular the VideoStream wrapper. use OpenCV's VideoCapture. check for isOpened() before trying to read. when reading with ok, frame = cap.read(), check if the read frame is ok, otherwise break the reading loop and discard the value in frame.

Cvlib not showing boxes, labels and confidence

I am trying to replicate a simple object detection that I found in on website.
import cv2
import matplotlib.pyplot as plt
import cvlib as cv
from cvlib.object_detection import draw_bbox
im = cv2.imread('downloads.jpeg')
bbox, label, conf = cv.detect_common_objects(im)
output_image = draw_bbox(im, bbox, label, conf)
plt.imshow(output_image)
plt.show()
All required libraries are installed and there are no errors running the code. However, it does not show the output image with the boxes, labels and confidence. How do I fix it?
#After loading an image use an assert:
img = cv2.imread('downloads.jpeg')
assert not isinstance(img,type(None)), 'image not found'

How to use Handwriting Recognition model as an API?

I have created one machine learning model using Tensorflow and Keras by using IAM dataset. How to load this model as an API to predict an image? When I was trying to integrate It shows error
return self.function(inputs, **arguments)
File "test2.py", line 136, in resize_image
return tf.image.resize_images(image,[56,56])
NameError: name 'tf' is not defined
I have load model using from keras.models import load_model and trying to predict image handwriting. low_loss.hdf5 is model which I try to integrate.
def testmodel(image_path):
global model
# load the pre-trained Keras model
model = load_model('low_loss.hdf5')
model.summary()
img = Image.open(image_path).convert("L")
img = np.resize(image_path, (28,28,1))
im2arr = np.array(img)
im2arr = im2arr.reshape(1,28,28,1)
y_pred = model.predict_classes(im2arr)
return y_pred
I wish to predict image Handwritten data.
your error is about tf which is not loaded.
try:
import tensorflow as tf
You were getting an error because you have not imported TensorFlow in your code or if you have imported you have not given an alias.
import tensorflow as tf

Categories

Resources