Roboflow Code Error not able to Run it on numpy images - python

I created a program that would decompile a video, run a roboflow yolo v5 model on each frame and then recompile the video. I am getting an error when saving the last frame of the model. Also I want to integrate the loop which opens the file and then runs the model instead of saving the video frame by frame and then running the model on the saved frames... This version of the code provided is where I tried to integrate the loops and I get an error during the second loop:
import cv2
from roboflow import Roboflow
import numpy as np
import cv2
import skvideo.io
import numpy as np
rf = Roboflow(api_key="API_KEY")
project = rf.workspace("WORKSPACE_NAME").project("PROJECT_NAME")
model = project.version(4).model
vidcap = cv2.VideoCapture('video2.mp4') #runs video file
success,image = vidcap.read() #creates two variables success and image, success is a boolean that returns true
count = 0 # as long as videocapture returns another image
while success:
#cv2.imwrite("/content/frames2/frame%d.jpg" % count, image) # save frame as JPEG file
success,image = vidcap.read()
#print('Read a new frame: ', success)
count += 1
height,width,layers=cv2.image.shape
array=np.zeros(shape=(count, height, width, layers))
print(count)
for i in range(count):
name = "/content/frames2/frame"+str(i)+".jpg" #run model on each frame of video
success,image=vidcap.read()
prediction=model.predict((image), confidence=40, overlap=30)
prediction.save(name)
print(i)
height,width,layers=cv2.imread(name).shape
array=np.zeros(shape=(count, height, width, layers))
for j in range(0,count):
foto=cv2.imread("/content/frames2/frame"+str(j)+".jpg")
#print(foto.shape)
array[j]=foto
array = array.astype(np.uint8)
skvideo.io.vwrite("codevideo.mp4", array)
This is the error I'm getting:
ValueError Traceback (most recent call last)
<ipython-input-2-c47bf522ab91> in <module>
23 name = "/content/frames2/frame"+str(i)+".jpg" #run model on each frame of video
24 success,image=vidcap.read()
---> 25 prediction=model.predict((image), confidence=40, overlap=30)
26 prediction.save(name)
27 print(i)
/usr/local/lib/python3.8/dist-packages/roboflow/models/object_detection.py in predict(self, image_path, hosted, format, classes, overlap, confidence, stroke, labels)
176 image_dims = {"width": "0", "height": "0"}
177 else:
--> 178 raise ValueError("image_path must be a string or a numpy array.")
179 else:
180 # Create API URL for hosted image (slightly different)
ValueError: image_path must be a string or a numpy array.
I created a program that would decompile a video, run a roboflow yolo v5 model on each frame and then recompile the video. I am getting an error when saving the last frame of the model. I thought I would be able to run the model on each frame saved locally in the numpy array but it throws the error detailed above...

Related

I created protobuf model file in TensorFlow and used it in python with opencv package. Why is this not working?

I tried to use .pb and .pbtxt files with the python OpenCV package. But it didn't work. Here is my code,
import cv2
# Load a model imported from Tensorflow
tensorflowNet = cv2.dnn.readNetFromTensorflow('saved_model.pb', 'label_map.pbtxt')
# Input image
img = cv2.imread('image (60).png')
rows, cols, channels = img.shape
# Use the given image as input, which needs to be blob(s).
tensorflowNet.setInput(cv2.dnn.blobFromImage(img, size=(640, 640), swapRB=True, crop=False))
# Runs a forward pass to compute the net output
networkOutput = tensorflowNet.forward()
# Loop on the outputs
for detection in networkOutput[0, 0]:
score = float(detection[2])
if score > 0.2:
left = detection[3] * cols
top = detection[4] * rows
right = detection[5] * cols
bottom = detection[6] * rows
# draw a red rectangle around detected objects
cv2.rectangle(img, (int(left), int(top)), (int(right), int(bottom)), (0, 0, 255), thickness=2)
# Show the image with a rectagle surrounding the detected objects
cv2.imshow('Image', img)
cv2.waitKey()
cv2.destroyAllWindows()
Here is the error,
[libprotobuf ERROR D:\a\opencv-python\opencv-python\opencv\3rdparty\protobuf\src\google\protobuf\wire_format_lite.cc:581] String field 'opencv_tensorflow.FunctionDef.Node.ret' contains invalid UTF-8 data when parsing a protocol buffer. Use the 'bytes' type if you intend to send raw bytes.
Traceback (most recent call last):
File "D:\Disk_4\Python Projects\FDAM\main.py", line 14, in <module>
tensorflowNet = cv2.dnn.readNetFromTensorflow('saved_model.pb', 'label_map.pbtxt')
cv2.error: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\dnn\src\tensorflow\tf_io.cpp:42: error: (-2:Unspecified error) FAILED: ReadProtoFromBinaryFile(param_file, param). Failed to parse GraphDef file: saved_model.pb in function 'cv::dnn::ReadTFNetParamsFromBinaryFileOrDie'
I found this code in https://jeanvitor.com/tensorflow-object-detecion-opencv/ and changed it with my file names.
I trained the model and generated this saved_model.pb file in TensorFlow.
Why is this code not working? please help me. This is my first project with machine learning.

Assertion failed while exporting depth images from h5 file

Th h5 file does not have a group or subgroup, and when I try to extract images it shows me this error. These are depth images.
This code works for h5 file with group i.e. images then I just write image_ds = hf['images'] and it works, but for h5 file without group doesn't work.
Maybe some error in imwrite function, because when I print(IMAGE_arr) and print(imagename) it prints fine. The number of dimensions are 3 and type is float32
Here is my code:
import h5py
import numpy as np
import cv2
save_dir = 'C:/Users.../depth_imgs'
with h5py.File('depth.h5', 'r') as hf:
image_ds = hf
for imagename in image_ds.keys():
IMAGE_arr = image_ds[imagename][()]
cv2.imwrite(f"{save_dir}/{imagename}", IMAGE_arr)
cv2.waitKey(1000)
cv2.destroyAllWindows()
Loaded data:
enter image description here
enter image description here

How to convert a batch of image frames to a video of the same quality

I used this script to convert an 18-second video to its frames. In total, I got 558 frames.
import cv2, os
import numpy as np
from os.path import isfile, join
vid_cap = cv2.VideoCapture('PXL_20211116_020738341.mp4')
success,image = vid_cap.read()
count = 0
while success:
cv2.imwrite("frame%d.jpg" % count, image) # save frame as JPEG file
success,image = vid_cap.read()
print('Read a new frame: ', success)
count += 1
The output (558 frames) is basically the same video with some masks. I want to turn those frames into a new video that is also 18 seconds. Meaning, I want the original video and the output video to run in the same motion quality. How can I achieve this with Python?
You may try this.
Read your jpeg file to an images array and use VideoWriter.
You are also able to set fps (maybe 558/18) to this function.
https://theailearner.com/2018/10/15/creating-video-from-images-using-opencv-python/

Unable to read .npz file in python

I created an npz(compressed file) using following code:
import pandas as pd
from tqdm import tqdm
from keras.preprocessing import image
import numpy as np # for mathematical operations
from keras.preprocessing import image # for preprocessing the images
anomoly_data = pd.read_csv('/code_data/anomoly_data.csv')
f = open('/code_data/anomoly_data.npz', 'wb')
for i in tqdm(range(anomoly_data.shape[0])):
img = image.load_img(anomoly_data['image'][i], target_size=(224,224,3))
# converting it to array
img = image.img_to_array(img)
# normalizing the pixel value
img = img/255
np.savez_compressed(f, img)
f.close()
the compressed file has size of 52GBs which indicates that it contains data of all the images.
in anomoly_data.csv i have the path for each image and entries in .csv are
188604
and now i'm trying to read the data of all the images from compressed using following code:
f=open("/code_data/anomoly_data.npz","rb")
for i in tqdm(range(anomoly_data.shape[0])):
img=np.load(f)
print(img)
and then I get this error telling me that there is only one ndarray in the compressed file, Error:
<numpy.lib.npyio.NpzFile object at 0x7fdfd1d437c0>
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-7-29b2878e9fe9> in <module>
1 f=open("/code_data/anomoly_data.npz","rb")
2 for i in range(anomoly_data.shape[0]):
----> 3 img=np.load(f)
4 print(img)
5
~/Programs/Anaconda/lib/python3.8/site-packages/numpy/lib/npyio.py in load(file, mmap_mode, allow_pickle, fix_imports, encoding)
442 # Try a pickle
443 if not allow_pickle:
--> 444 raise ValueError("Cannot load file containing pickled data "
445 "when allow_pickle=False")
446 try:
ValueError: Cannot load file containing pickled data when allow_pickle=False
but i get data of only one array which suggests that there is either data of only one image or it is deformed. implying that compressed file is not genrated properly.
is there any better way to do this?

Unable to train images using LBPHFaceRecognizer and opencv

Currently, I am working on training the images for facial recognition system. I am using Python, OpenCV for doing so. I have collected the samples from the webcam, however, the size of sample images differs. The example for the size of sample images is 376 x 376, 412 x 412, 836 x 836.
The screenshot of current working directory:
The sample images are saved within the main folder named 'sampleImgFolder' and under the main folder specific folder for each sample.
Source code for training image
import os
import cv2
import numpy as np
from PIL import Image
recognizer = cv2.face.LBPHFaceRecognizer_create()
targetImagesDirectory="sampleImgFolder/"
dataset = cv2.CascadeClassifier('resources/haarcascade_frontalface_default.xml')
def getImageWithID(path):
#empty list to store processed data
sampleFaces = []
sampleFaceId = []
os.chdir(targetImagesDirectory)
for directory in os.listdir():
os.chdir(directory)
for files in os.listdir():
imagePath = '{}/{}'.format(os.getcwd(), files)
imagePil = Image.open(imagePath).convert('L')
imageNumpy = np.array(imagePil, 'uint8') #conversion of normal image to numpy array
#imageNumpy.astype(np.float32)
#detect face
faces = dataset.detectMultiScale(imageNumpy)
#extracting id from file name
id = files.split('_')
id = id[0].split('-')
id = id[2]
for (x, y, w, h) in faces:
sampleFaces.append(imageNumpy[y:y + h, x:x + w])
sampleFaceId.append(id)
os.chdir('../')
os.chdir('../')
return np.array(sampleFaceId), sampleFaces
print("reading images")
Ids,faces=getImageWithID(targetImagesDirectory)
print('reading completed')
recognizer.train(faces,Ids)
print("training")
#train the dataset. Create a file name trainningData.yml
recognizer.write('train/trainningData.yml')
cv2.destroyAllWindows()
I am getting following error while running above code:
That is because the datatype of Ids is a list[str]. .train() methods accepts int for labels

Categories

Resources