The problem is, program is really lengthy and computationally expensive.
so is there any way to make this program faster or any other way to write this code?
I am beginner in python and would love to take all suggestions or different approach then this program
also i am new to the stack overflow so if anything is wrong in this post or any issue in program please point out in comment .
first section of code is
#TEST V2.1 multitracker
import cv2
import numpy as np
#path = (input("enter the video path: "))
cap = cv2.VideoCapture(" YOUR VIDEO PATH ")
# creating the dictionary to add all the wanted trackers in OpenCV that can be used for tracking in future
OBJECT_TRACKING_MACHINE = {
"csrt": cv2.legacy.TrackerCSRT_create,
"kcf": cv2.legacy.TrackerKCF_create,
"boosting": cv2.legacy.TrackerBoosting_create,
"mil": cv2.legacy.TrackerMIL_create,
"tld": cv2.legacy.TrackerTLD_create,
"medianflow": cv2.legacy.TrackerMedianFlow_create,
"mosse": cv2.legacy.TrackerMOSSE_create
}
# Creating the MultiTracker variable object to store the
trackers = cv2.legacy.MultiTracker_create()
here I started the loop
while True:
frame = cap.read()[1]
#print("freame start",frame)
if frame is None:
print("error getting the video,please check the input")
break
frame = cv2.resize(frame,(1080,720))
Thresh = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
#Thresh = cv2.adaptiveThreshold(gray, 185, cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY_INV, 11, 6)
#print(trackers.update(Thresh))
(success, boxes) = trackers.update(Thresh)
# loop over the bounding boxes and draw them on the frame
if success == False:
bound_boxes = trackers.getObjects()
idx = np.where(bound_boxes.sum(axis= 1) != 0)[0]
bound_boxes = bound_boxes[idx]
trackers = cv2.legacy.MultiTracker_create()
for bound_box in bound_boxes:
trackers.add(tracker,Thresh,bound_box)
x,y,w,h = cv2.boundingRect(Thresh)
k = cv2.waitKey(50)
And I am guessing this is the section which is making the program slow
if is there any different way to represent this part or any idea different then this
for i,box in enumerate(boxes):
(x, y, w, h) = [int(v) for v in box]
#cv2.rectangle(Thresh, (x, y), (x + w, y + h), (255, 255, 255), 2)
#cv2.putText(Thresh,('TRACKING BOX NO-'+str(i)),(x+10,y-3),cv2.FONT_HERSHEY_PLAIN,1.0,(255,255,0),2)
arr = boxes.astype(int)
if i == 0 :
Roi = Thresh[(arr[i,1]):(arr[i,1]+arr[i,3]),(arr[i,0]):(arr[i,0]+arr[i,2])]
murg = cv2.resize(Roi,(300,200))
cv2.imshow("horizon", murg)
#print(murg)
if i == 1 :
Roi1 = Thresh[(arr[i,1]):(arr[i,1]+arr[i,3]),(arr[i,0]):(arr[i,0]+arr[i,2])]
Roi = Thresh[(arr[(i-1),1]):(arr[(i-1),1]+arr[(i-1),3]),(arr[(i-1),0]):(arr[(i-1),0]+arr[(i-1),2])]
murg = cv2.resize(Roi,(300,200))
murg1 = cv2.resize(Roi1,(300,200))
hori = np.concatenate((murg,murg1),axis=1)
cv2.imshow("horizon",hori)
#print(hori)
elif i == 2 :
Roi2 = Thresh[(arr[i,1]):(arr[i,1]+arr[i,3]),(arr[i,0]):(arr[i,0]+arr[i,2])]
Roi1 = Thresh[(arr[(i-1),1]):(arr[(i-1),1]+arr[(i-1),3]),(arr[(i-1),0]):(arr[(i-1),0]+arr[(i-1),2])]
Roi = Thresh[(arr[(i-2),1]):(arr[(i-2),1]+arr[(i-2),3]),(arr[(i-2),0]):(arr[(i-2),0]+arr[(i-2),2])]
murg = cv2.resize(Roi,(300,200))
murg1 = cv2.resize(Roi1,(300,200))
murg2 = cv2.resize(Roi2,(300,200))
hori = np.concatenate((murg,murg1,murg2),axis=1)
cv2.imshow("horizon",hori)
#print(hori)
elif i == 3 :
Roi3 = Thresh[(arr[i,1]):(arr[i,1]+arr[i,3]),(arr[i,0]):(arr[i,0]+arr[i,2])]
Roi2 = Thresh[(arr[(i-1),1]):(arr[(i-1),1]+arr[(i-1),3]),(arr[(i-1),0]):(arr[(i-1),0]+arr[(i-1),2])]
Roi1 = Thresh[(arr[(i-2),1]):(arr[(i-2),1]+arr[(i-2),3]),(arr[(i-2),0]):(arr[(i-2),0]+arr[(i-2),2])]
Roi = Thresh[(arr[(i-3),1]):(arr[(i-3),1]+arr[(i-3),3]),(arr[(i-3),0]):(arr[(i-3),0]+arr[(i-3),2])]
murg = cv2.resize(Roi,(300,200))
murg1 = cv2.resize(Roi1,(300,200))
murg2 = cv2.resize(Roi2,(300,200))
murg3 = cv2.resize(Roi3,(300,200))
hori = np.concatenate((murg,murg1,murg2,murg3),axis=1)
cv2.imshow("horizon",hori)
#print(hori)
this section is so that I can print the ROI matrix and to select the ROI
if k == ord("1"):
print(murg)
if k == ord("2"):
print(murg1)
if k == ord ("3"):
print(murg2)
if k == ord("4"):
print(murg3)
cv2.imshow('Frame', Thresh)
if k == ord("e"):
break
if k == ord("s"):
roi = cv2.selectROI("Frame", Thresh, fromCenter=False,showCrosshair=False)
tracker = OBJECT_TRACKING_MACHINE['mosse']()
trackers.add(tracker, Thresh, roi)
#print(boxes,success)
cap.release()
cv2.destroyAllWindows()
when you will run this code you can extract 4 ROI frames which will track your ROI's (I haven't added the precaution for empty matrix so it will give you error if you select more than 4 roi's)
my end goal is to extract those ROI videos for Image processing (this code is not done yet and there's more image processing is going to happen in letter part) **
I'm coding a Python program to automate image processing tasks (threshold images, detect borders, optical flow, etc.) using PySimpleGUI, openCV and Numpy. However, when I run all the code and press the button that takes me to the function to obtain the image with border detection through the graphical interface, only a black box appears, but implementing only the border detection function by itself seems to work without any problem. Here is the code of the border detection function:
def correl_Sobel(image_filename=None):
image = cv2.imread(image_filename)
img_gray = (image[:,:,0]/3+image[:,:,1]/3+image[:,:,2]/3)
img_th = img_gray
img_th[img_th < 120] = 0
img_th[img_th != 0] = 255
img_th = abs(img_th)
vertFilter =[[-1,-2,-1],[0,0,0],[1,2,1]]
horiFilter = [[-1,0,1],[-2,0,2],[-1,0,1]]
img_edges = np.zeros_like(image)
n, m = img_gray.shape
for x in range(3, n-2):
for y in range(3, m-2):
pixels = img_th[x-1:x+2, y-1:y+2]
verticalPixels = vertFilter*pixels
vertScore = (verticalPixels.sum()+4)/8
horizontalPixels = horiFilter*pixels
horiScore = (horizontalPixels.sum()+4)/8
#formula
edgeScore = (vertScore**2 + horiScore**2)**0.5
#three-layered images
img_edges[x,y] = [edgeScore]*3
#pixel values / maximum value
img_edges = img_edges/img_edges.max()
return img_edges
And here is the code for the GUI and button implementation:
prev_filename = correlated = thresholded = convolution = None
while True:
#read window
event, values = window.read()
if event == '-EXIT-' or event == sg.WIN_CLOSED:
break
if event == '-FOLDER-':
folder = values['-FOLDER-']
img_types = (".png", ".jpg", "jpeg", ".tiff", ".bmp")
try:
flist0 = os.listdir(folder)
except:
continue
fnames = [f for f in flist0 if os.path.isfile(
os.path.join(folder, f)) and f.lower().endswith(img_types)]
#update file directory
window['-FILE LIST-'].update(fnames)
#Select a file
elif event == '-FILE LIST-':
try:
filename = os.path.join(values['-FOLDER-'], values['-FILE LIST-'][0])
image = cv2.imread(filename)
window['-IN-'].update(data=cv2.imencode('.png', image)[1].tobytes())
window['-OUT-'].update(data='')
except:
continue
#edge detection button
elif event == '-CONV-':
try:
if values['-FILE LIST-']:
filename = os.path.join(values['-FOLDER-'], values['-FILE LIST-'][0])
correlated = correl_Sobel(filename)
# plt.imshow(correlated)
window['-IN-'].update(data=cv2.imencode('.png', image)[1].tobytes())
window['-OUT-'].update(data=cv2.imencode('.png',correlated)[1].tobytes())
else:
continue
except:
continue
Here's what happens when I press the edge detection button: screenshot
And here's what it should look like (I got this image by running only the function above): edgeDetectedImg
Thanks in advance
I got 2 functions to get the values of the HDD drives. I am using tkinter and I can access the graphics with the buttons. But, after I click on one, I can't seem to remove the previous.
The objective is: if I click the "C Drive" button, I erase the E graph, and if I click on the "E" drive, I erase the C graph.
#Disk E Storage
def hdd_e():
usage_e=shutil.disk_usage("E:\\")
total_space_e = usage_e[0]
used_space_e = usage_e[1]
free_space_e =usage_e[2]
fig_e = matplotlib.figure.Figure(figsize=(50, 5), facecolor="#F0F0F0")
canvas_e = FigureCanvasTkAgg(fig_e, master=tab3)
ax_e = fig_e.add_subplot(111)
ax_e.pie([total_space_e, used_space_e, free_space_e])
ax_e.legend(["Total", "Used", "Free"])
circle_e = matplotlib.patches.Circle((0, 0), 0)
ax_e.add_artist(circle_e)
canvas_e.get_tk_widget().pack()
canvas_e.draw()
#Disk C Storage
def hdd_c():
usage_c=shutil.disk_usage("C:\\")
total_space_c = usage_c[0]
used_space_c = usage_c[1]
free_space_c =usage_c[2]
fig_c = matplotlib.figure.Figure(figsize=(50, 5), facecolor="#F0F0F0")
canvas_c = FigureCanvasTkAgg(fig_c, master=tab3)
ax_c = fig_c.add_subplot(111)
ax_c.pie([total_space_c, used_space_c, free_space_c ])
ax_c.legend(["Total", "Used", "Free"])
circle_c = matplotlib.patches.Circle((0, 0), 0)
ax_c.add_artist(circle_c)
canvas_c.get_tk_widget().pack()
canvas_c.draw()
The Code:
I am attempting to construct a python script that takes the RGB values of an image, along with the contours width and height into a neural network to determine if the little rc car I have constructed should go forward or turn right.
Code:
import cv2
cam = cv2.VideoCapture(2)
class Pic_Capture:
def __init__(self):
# print("Fuck you")
self.i = 0
def Img_Mods(self, cam):
self.ret, self.frame = cam.read()
self.buf1 = cv2.flip(self.frame, 1)
# self.buf2 = cv2.cvtColor(self.buf1, cv2.COLOR_BGR2RGB)
self.buf3 = cv2.GaussianBlur(self.buf1,(5,5),0)
cv2.imshow('buf2', self.buf3)
def Measurement_Bullshit(self):
self.buf4 = cv2.cvtColor(self.buf3, cv2.COLOR_RGB2GRAY)
self.buf5 = cv2.adaptiveThreshold(self.buf4,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,11,2)
_, contours, hierarchy = cv2.findContours(self.buf5, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
for cnt in contours:
M = cv2.moments(cnt)
self.cx = int(M['m10']/M['m00'])
self.cy = int(M['m01']/M['m00'])
print(self.cx, self.cy)
cv2.imshow('buf4', self.buf4)
running = True
i = 0
Camera = Pic_Capture()
while running:
Camera.Img_Mods(cam)
if i % 30 == 0 and i != 0:
Camera.Off_and_On_Again()
Camera.Measurement_Bullshit()
i += 1
print(i)
if cv2.waitKey(1) == 27:
Camera.Stop_Code()
Question 1:
I consistently receive this within my console and I would like to get to rid of it for debugging later down the line.
Corrupt JPEG data: 4 extraneous bytes before marker 0xd5
Every time this pops up it is at a different marker, so I do not know how to not print that to the console.
Question 2:
After the code within the if statement: if i % 10 == 0 and i != 0: runs the code spits back this error.
File "Nueral_Network_Creation.py", line 23, in Measurement_Bullshit self.cx = int(M['m10']/M['m00']) ZeroDivisionError: float division by zero
I understand that it is most likely that the code is not finding enough contours to be run properly or maybe it is something completely different. As always any help is much appreciated.
I've gotten OpenCV working with Python and I can even detect a face through my webcam. What I really want to do though, is see movement and find the point in the middle of the blob of movement. The camshift sample is close to what I want, but I don't want to have to select which portion of the video to track. Bonus points for being able to predict the next frame.
Here's the code I have currently:
#!/usr/bin/env python
import cv
def is_rect_nonzero(r):
(_,_,w,h) = r
return (w > 0) and (h > 0)
class CamShiftDemo:
def __init__(self):
self.capture = cv.CaptureFromCAM(0)
cv.NamedWindow( "CamShiftDemo", 1 )
self.storage = cv.CreateMemStorage(0)
self.cascade = cv.Load("/usr/local/share/opencv/haarcascades/haarcascade_mcs_upperbody.xml")
self.last_rect = ((0, 0), (0, 0))
def run(self):
hist = cv.CreateHist([180], cv.CV_HIST_ARRAY, [(0,180)], 1 )
backproject_mode = False
i = 0
while True:
i = (i + 1) % 12
frame = cv.QueryFrame( self.capture )
if i == 0:
found = cv.HaarDetectObjects(frame, self.cascade, self.storage, 1.2, 2, 0, (20, 20))
for p in found:
# print p
self.last_rect = (p[0][0], p[0][1]), (p[0][2], p[0][3])
print self.last_rect
cv.Rectangle( frame, self.last_rect[0], self.last_rect[1], cv.CV_RGB(255,0,0), 3, cv.CV_AA, 0 )
cv.ShowImage( "CamShiftDemo", frame )
c = cv.WaitKey(7) % 0x100
if c == 27:
break
if __name__=="__main__":
demo = CamShiftDemo()
demo.run()
Found a solution at How do I track motion using OpenCV in Python?