Arrays of object detected as int output - python

I would like to gain out the arrays of object detected using python and YOLOv5. I use:
import torch
import cv2
from matplotlib import pyplot as plt
import numpy as np
import uuid
import os
import time
model = torch.hub.load('ultralytics/yolov5', 'custom', path="C:\\Users\\Legion\\Desktop\\yolo\\yolov5\\runs\\train\\exp5\\weights\\last.pt", force_reload=True)
cap = cv2.VideoCapture(0)
while cap.isOpened():
ret, frame = cap.read()
results = model(frame)
cv2.imshow('YOLO', np.squeeze(results.render()))
if cv2.waitKey(10) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
How to get the arrays of objects detected and how to get them as integers, to provide comparison? F.e. If arrays [x,y] < [70,90] then ...
Thank you guys.
I have searched a lot and now I need specific idea how to get the results I need.

Related

Making a real-time graph using real-time data from python code

I'm doing some python work for real-time video analysis.
About for 1 month... I've revised same code everyday, but there was no improvement.
So I decided to write a question here.
**My Question : **
I finally got a real-time data (max intensity) from video camera, but I failed to draw a real-time graph using this data.
I tried to use Pipe(multiprocessing), but everytime I try this the python says '!_src.empty() in function 'cv::cvtColor', and other errors...
This is a code for extracting a max intensity from camera(video) in realtime.
import numpy as np
import cv2
import matplotlib.pyplot as plt
cap = cv2.VideoCapture(0)
fps = cap.get(cv2.CAP_PROP_FPS)
count = 0
while(True):
ret, frame = cap.read()
vid = cv2.cvtColor(frame, cv2.IMREAD_COLOR)
cv2.imshow('Interference',vid)
h, s, v = cv2.split(vid)
if(int(cap.get(1)) % fps == 0):
for i in np.arange(fps):
print(np.max(v))
if cv2.waitKey(1) & 0xFF == ord('q'):
break
And this is my failed code to extract the realtime data and make a realtime graph.
import numpy as np
import cv2
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from itertools import count
from multiprocessing import Process, Pipe
cap = cv2.VideoCapture(0)
fps = cap.get(cv2.CAP_PROP_FPS)
plt.style.use('fivethirtyeight')
count = 0
while(True):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('gray', gray)
h, s, v = cv2.split(vid)
def func0(pipe):
if(int(cap.get(1)) % fps == 0):
px = np.max(v)
for i in np.arange(fps):
pipe.send(px)
pipe.close(px)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
def func1(pipe):
x_val = []
y_val = []
def animate():
x_val.append(count())
y_val.append(pipe)
plt.cla()
plt.plot(x_val, y_val)
ani = FuncAnimation(plt.gcf(),animate,interval = 1000)
plt.tight_layout()
plt.show()
if __name__ == '__main__':
a_pipe, b_pipe = Pipe()
p0 = Process(target=func0, args=(a_pipe,))
p0.start()
p1 = Process(target=func1, args=(b_pipe.recv(),))
p1.start()
p0.join()
p1.join()
How can I try to draw a realtime graph from this data ?
If you give me any hint of this, I will really appreciated about it.
Thank you.
I tried some code for extracting a realtime data and making a realtime graph.
Finally I succeed to extract a realtime data from video, but failed the other one.
Can you please help me to make a realtime graph using the extracted data in realtime?

extract the main heading from the image using openCV and Tesseract in python

now i want to use openCV and tesseract to extract the title of the image.
Expected Output --
[ Demo Pharma Distributors ]
Please Help
As I said, the code was used for something else but I think it can give you a nice piece of code to work with.
import numpy as np
from PIL import ImageGrab
from PIL import Image
import cv2
import time
import pytesseract
p1 = 520
p2 = 760
p3 = 1450
p4 = 830
# Those points are pixel points you will need to adjust for your frame
while(True):
frame = np.array(ImageGrab.grab(bbox=(p1 ,p2 , p3, p4)))
frame = cv2.bitwise_not(frame)
cv2.imshow("window", frame)
pytesseract.pytesseract.tesseract_cmd = r'Path\to\tesseract.exe'
txt = pytesseract.image_to_string(frame)
str_num = re.findall(r'.*', txt)
print(txt)
print(str_num)
print("-----------")
if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break

ValueError: cannot reshape array of size 270000 into shape (1,100,100,3)

I am trying to predict my hand posture using a live video. But when I compile my program this error occurs " ValueError: cannot reshape array of size 270000 into shape (1,100,100,3) "
import traceback
import cv2
import numpy as np
import math
from numpy import loadtxt
from keras.models import load_model
cam = cv2.VideoCapture(0)
classifier = load_model('hand_gest.h5')
while(1):
try:
ret, frame = cam.read()
frame = cv2.flip(frame,1)
roi = frame[100:400,200:500]
cv2.rectangle(frame,(200,100),(500,400),(0,255,0),2)
cv2.imshow('curFrame',frame)
cv2.imshow('roi',roi)
classifier.compile(loss='binary_crossentropy',optimizer='rmsprop',metrics=['accuracy'])
img = cv2.resize(roi,(100,100))
img = np.reshape(roi,[1,100,100,3])
classes = classifier.predict_classes(img)
print(classes)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
except Exception:
traceback.print_exc()
pass
cam.release()
cv2.destroyAllWindows()
How do I reshape it in such way the it does not show error? Can you please explain why that error occurred?
Well I guess the error comes from this line img = np.reshape(roi,[1,100,100,3]) and this is because your roi (= frame[100:400,200:500]) has (300,300) shape. And I see in the line above that your are using an other variable img which has the correct shape (100,100,3). So try this instead :
img = cv2.resize(roi,(100,100))
img = np.reshape(img,[1,100,100,3])
Furthermore, I don't think compiling your model in a while loop is something you want. It takes some time and it is unnecessary. So put it before the while loop and after loading weights.

Not receving continous frame from opencv

my function did not return continuous frame ,it return only signal frame and then break
here is my code:
import cv2
import numpy as np
def video():
cam=cv2.VideoCapture(0)
while cam.isOpened:
_,frame=cam.read()
return frame
im=video()
cv2.imshow("image",im)
cv2.waitKey(0)
I want such function ,when i call this function ,anywhere in my code it return a continues frame and i do stuff on it whatever display ,face detection or other stuff
Try to gather the frames in a while loop without breaking it by returning:
import cv2
import numpy as np
def video(cam):
_,frame=cam.read()
return frame
cam=cv2.VideoCapture(0)
while cam.isOpened:
im=video(cam)
cv2.imshow("image",im)
cv2.waitKey(0)
When you return, you just exit the while loop, so it just shows 1 image.
import cv2
import numpy as np
cam=cv2.VideoCapture(0)
while cam.isOpened:
_,frame=cam.read()
cv2.imshow("image",frame)
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break
cap.release()
cv2.destroyAllWindows()

getting the image from VideoCapture object using read()

I am reading a video frame by frame using:
vc = cv2.VideoCapture('test.avi')
and later on I check whether the frame has been read using:
if vc.isOpened():
rval,frame = vc.read()
else:
rval = False
Now the problem is , if I try to convert this frame to a numpy array using the following code:
PILImage = Image.fromstring("L",cv.GetSize(frame),frame.tostring())
NumPyArray = np.array(PILImage)
I get an error saying:
CvArr argument 'arr' must be IplImage, CvMat or CvMatND. Use fromarray() to convert numpy arrays to CvMat or cvMatND
From Documentation of VideoCapture::read I found that it returns two things [retVal,Image] . How do I get the Image part only and how do I convert it to Numpy Array?
Further Info:
Basically the whole point in doing this is that ,I am trying to write a program which allows me to go through each frame of the video by pressing spacebar, and select a particular region from any frame and save it as a jpg. Here is the Code:
from ITMS import ITMS
import cv2
from cv2 import cv
import numpy as np
import matplotlib.pyplot as plt
import Image
import matplotlib.widgets as widgets
def onselect(eclick, erelease):
if eclick.ydata>erelease.ydata:
eclick.ydata,erelease.ydata=erelease.ydata,eclick.ydata
if eclick.xdata>erelease.xdata:
eclick.xdata,erelease.xdata=erelease.xdata,eclick.xdata
ax.set_ylim(erelease.ydata,eclick.ydata)
ax.set_xlim(eclick.xdata,erelease.xdata)
fig.canvas.draw()
def subImager(arr):
fig = plt.figure()
ax = fig.add_subplot(111)
plt_image=plt.imshow(arr,cmap="Greys_r")
rs=widgets.RectangleSelector(
ax, onselect, drawtype='box',
rectprops = dict(facecolor='red', edgecolor = 'red', alpha=0.2, fill=True))
plt.show()
cv2.namedWindow("preview")
vc = cv2.VideoCapture('test.avi')
if vc.isOpened():
rval,frame = vc.read()
else:
rval = False
while rval:
key = cv2.waitKey(30)
if key==32:
cv2.imshow("preview", frame)
NumPyArray=ITMS.CVtoNPArray(frame)
subImager(NumPyArray)
rval,frame = vc.read()
elif key==27:
break
cv2.destroyAllWindows()
ITMS Class:
from cv2 import cv
import cv2
import numpy as np
from PIL import Image
class ITMS:
def __init__(self):
pass
def CVtoNPArray(CVImage):
PILImage = Image.fromstring("L",cv.GetSize(CVImage),CVImage.tostring())
NumPyArray = np.array(PILImage)
return NumPyArray
CVtoNPArray=staticmethod(CVtoNPArray)
You are over-complicating things, the image returned in your frame name is already a numpy array. If you want to convert it to PIL, simply do Image.fromarray(frame).

Categories