I am trying to execute this code but I am getting the below stated error. It says that the array size doesn't match. I have tried a few solutions given on the internet. Changed the values of the color and also tried by removing the np.array from the code in the concerned lines
skin_ycrcb_min = np.array((0, 138, 67))
skin_ycrcb_max = np.array((255, 173, 133))
Bellow is the code:
import cv2
import numpy as np
import util as ut
import svm_train as st
import re
model=st.trainSVM(17)
#create and train SVM model each time coz bug in opencv 3.1.0 svm.load()
https://github.com/Itseez/opencv/issues/4969
cam=int(raw_input("Enter Camera number: "))
cap=cv2.VideoCapture(cam)
font = cv2.FONT_HERSHEY_SIMPLEX
def nothing(x) :
pass
text= " "
temp=0
previouslabel=None
previousText=" "
label = None
while(cap.isOpened()):
_,img=cap.read()
cv2.rectangle(img,(900,100),(1300,500),(255,0,0),3) # bounding box which
captures ASL sign to be detected by the system
img1=img[100:500,900:1300]
img_ycrcb = cv2.cvtColor(img1, cv2.COLOR_BGR2YCR_CB)
blur = cv2.GaussianBlur(img_ycrcb,(11,11),0)
skin_ycrcb_min = np.array((0, 138, 67))
skin_ycrcb_max = np.array((255, 173, 133))
mask = cv2.inRange(blur, skin_ycrcb_min, skin_ycrcb_max); # detecting the
hand in the bounding box using skin detection
contours,hierarchy = cv2.findContours(mask.copy(),cv2.RETR_EXTERNAL, 2)
cnt=ut.getMaxContour(contours,4000) # using contours
to capture the skin filtered image of the hand
if cnt!=None:
gesture,label=ut.getGestureImg(cnt,img1,mask,model) # passing the
trained model for prediction and fetching the result
if(label!=None):
if(temp==0):
previouslabel=label
if previouslabel==label :
previouslabel=label
temp+=1
else :
temp=0
if(temp==40):
if(label=='P'):
label=" "
text= text + label
if(label=='Q'):
words = re.split(" +",text)
words.pop()
text = " ".join(words)
#text=previousText
print text
cv2.imshow('PredictedGesture',gesture) # showing the best
match or prediction
cv2.putText(img,label,(50,150), font,8,(0,125,155),2) # displaying the
predicted letter on the main screen
cv2.putText(img,text,(50,450), font,3,(0,0,255),2)
cv2.imshow('Frame',img)
cv2.imshow('Mask',mask)
k = 0xFF & cv2.waitKey(10)
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
Error:
Traceback (most recent call last):
File "C:\Users\hlllv\Desktop\ASL\ASL-Translator-master\ASL.py", line 29, in
<module>
mask = cv2.inRange(blur, skin_ycrcb_min, skin_ycrcb_max); # detecting the
hand in the bounding box using skin detection
error: C:\build\2_4_winpack-bindings-win32-vc14-
static\opencv\modules\core\src\arithm.cpp:2711: error: (-209) The lower
bounary is neither an array of the same size and same type as src, nor a
scalar in function cv::inRange
Please help!
Related
code:
import face_recognition as fr
import os
import cv2
import face_recognition
import numpy as np
from time import sleep
def get_encoded_faces():
encoded = {}
for dirpath, dnames, fname in os.walk("./faces"):
for f in fname:
if f.endswith(".jpg") or f.endswith(".png"):
face = fr.load_image_file("faces/" + f)
encoding = fr.face_encodings(face)[0]
encoded[f.split(".")[0]] = encoding
return encoded, fname
def unknown_image_encoded(img):
face = fr.load_image_file("faces/" + img)
encoding = fr.face_encodings(face)[0]
return encoding
def classify_face(im):
faces, fname = get_encoded_faces()
faces_encoded = list(faces.values())
known_face_names = list(faces.keys())
img = cv2.imread(im, 1)
face_locations = face_recognition.face_locations(img)
unknown_face_encodings = face_recognition.face_encodings(img, face_locations)
face_names = []
for face_encoding in unknown_face_encodings:
matches = face_recognition.compare_faces(faces_encoded, face_encoding)
name = "Unknown"
face_distances = face_recognition.face_distance(faces_encoded, face_encoding)
best_match_index = np.argmin(face_distances)
if matches[best_match_index]:
name = known_face_names[best_match_index]
face_names.append(name)
for (top, right, bottom, left), name in zip(face_locations, face_names):
cv2.rectangle(img, (left-20, top-20), (right+20, bottom+20), (255, 0, 0), 2)
cv2.rectangle(img, (left-20, bottom -15), (right+20, bottom+20), (255, 0, 0), cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(img, name, (left -20, bottom + 15), font, 1.0, (255, 255, 255), 2)
return face_names, fname
cap = cv2.VideoCapture(0)
while True:
ret, image = cap.read()
recog, fname = classify_face(image)
print(recog)
cv2.imshow(fname, image)
if cv2.waitKey(1) & 0xFF == ord("q"):
break
video.release()
cv2.destroyAllWindows()
Error:
Traceback (most recent call last):
File "face.py", line 70, in <module>
recog, fname = classify_face(image)
File "face.py", line 37, in classify_face
img = cv2.imread(im, 1)
SystemError: <built-in function imread> returned NULL without setting an error
[ WARN:0] global C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-wbmte9m7\opencv\modules\videoio\src\cap_msmf.cpp (435) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback
The code works properly while using an image but now when I tried using it with video/real-time its throwing this error
I guess it requires the path instead of the image that is passed on to it, is there any other work around
I am trying to recognize faces in real time and the major issue with it was detecting unknown faces so when I started coding for real time I got this error.
The code and the error message don't agree. Are you running an older version of the code?
Error message:
File "face.py", line 37, in classify_face
img = cv2.imread(im, 1)
Code:
img = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
For debugging it may be helpful to display the received frame from the camera with code like the following:
ret, image = cap.read()
grey = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame', grey)
cv2.waitKey()
cv2.imread(im, 1) requires im to be the filename (datatype: string) of the image that you want to read.
Using cap = cv2.VideoCapture(0), you don't need to read images from files anymore, since the image that you want to classify is returned as an array from cap.read().
To fix your code for using cv2.VideoCapture, remove img = cv2.imread(im, 1) from your classify_face method and change the method definition to
def classify_face(img):
instead of
def classify_face(im):
Note, that the 0 option of cv2.VideoCapture refers to reading the live video stream from a camera with index 0.
I am using a fisheye camera for depth image. I had tried with StereoSGBM() method for the depth map which has not given the desired results but now I am trying it with StereoBM() method.
stereoMatcher = cv2.StereoBM() preset = 2 ndisparities = 32 WinSize = 5
if not left.grab() or not right.grab():
print("No more frames")
break
_, leftFrame = left.retrieve()
#leftFrame = cropHorizontal(leftFrame)
leftHeight, leftWidth = leftFrame.shape[:2]
_, rightFrame = right.retrieve()
#rightFrame = cropHorizontal(rightFrame)
rightHeight, rightWidth = rightFrame.shape[:2]
if (leftWidth, leftHeight) != imageSize:
print("Left camera has different size than the calibration data")
break
if (rightWidth, rightHeight) != imageSize:
print("Right camera has different size than the calibration data")
break
fixedLeft = cv2.remap(leftFrame, leftMapX, leftMapY, REMAP_INTERPOLATION)
fixedRight = cv2.remap(rightFrame, rightMapX, rightMapY, REMAP_INTERPOLATION)
grayLeft = cv2.cvtColor(fixedLeft, cv2.COLOR_BGR2GRAY)
grayRight = cv2.cvtColor(fixedRight, cv2.COLOR_BGR2GRAY)
As compute a function of StereoBM() also needs a third argument as disparity. I calculated it by using
grayLeft = cv2.cvtColor(fixedLeft, cv2.COLOR_BGR2GRAY)
grayRight = cv2.cvtColor(fixedRight, cv2.COLOR_BGR2GRAY)
disparity = stereoMatcher.compute(grayLeft, grayRight)
depth = stereoMatcher.compute(grayLeft, grayRight)
#cv2.imshow('left', fixedLeft)
#cv2.imshow('right', fixedRight)
cv2.imshow('depth', depth / DEPTH_VISUALIZATION_SCALE)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
left.release() right.release() cv2.destroyAllWindows()
But it is throwing an error which is:
Traceback (most recent call last):
File "fisheye_depth.py", line 104, in <module>
disparity = stereoMatcher.compute(grayLeft, grayRight)
TypeError: Incorrect type of self (must be 'StereoMatcher' or its derivative)
If anyone has any idea about it that what mistake I am doing. Let me know what mistake I am doing taking out the disparity for compute operator().
Ok, so I've been having a problem with some tracking code(I'm not too skilled, please bear this in mind). It is meant to track objects(Juggling balls) of one colour and output a .csv data file.
import cv2
import numpy as np
#h,s,v range of the object to be tracked
h,s,v,h1,s1,v1 = 31,91,0,74,255,255#GREEN
#h,s,v,h1,s1,v1 = 0, 161, 52, 26 ,255, 255 #Orange
#h,s,v,h1,s1,v1 = 90, 37, 0, 143, 180, 255 #Blue
threshold_value = 0
output_path = ('C:\\Python27')
cap = cv2.VideoCapture('Users\Tyson\Desktop\MillsMess.mp4')
#takes an image, and a lower and upper bound
#returns only the parts of the image in bounds
def only_color(frame, (b,r,g,b1,r1,g1)):
# Convert BGR to HSV
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# define range of blue color in HSV
lower = np.array([b,r,g])
upper = np.array([b1,r1,g1])
# Threshold the HSV image to get only blue colors
mask = cv2.inRange(hsv, lower, upper)
# Bitwise-AND mask and original image
res = cv2.bitwise_and(frame,frame, mask= mask)
return res, mask
#finds the largest contour in a list of contours
#returns a single contour
def largest_contour(contours):
c = max(contours, key=cv2.contourArea)
return c[0]
#takes an image and the threshold value returns the contours
def get_contours(im, threshold_value):
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
_ ,thresh = cv2.threshold(imgray,threshold_value,255,0)
_, contours, _ = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
return contours
#finds the center of a contour
#takes a single contour
#returns (x,y) position of the contour
def contour_center(c):
M = cv2.moments(c)
try: center = int(M['m10']/M['m00']), int(M['m01']/M['m00'])
except: center = 0,0
return center
frame_number = 0
positions = []
for i in range(1000000): positions.append((0,0))
#main loop of the program
while True:
#read image from the video
_, img = cap.read()
try: l = img.shape
except: break
#extract only the flesh tones
img, mask = only_color(img, (h,s,v,h1,s1,v1))
#find the contours in the image
contours = get_contours(img, threshold_value)
#if there are contours found in the image:
if len(contours)>0:
try:
#sort the contours by area
c = max(contours, key=cv2.contourArea)
img = cv2.drawContours(img, c ,-1, (0,0,255), 14)
positions[frame_number] = contour_center(c)
except: pass
frame_number += 1
#show the image and wait
#cv2.imshow('img', img)
cv2.imshow('img', cv2.resize(img, (480,700)))
k=cv2.waitKey(1)
if k==27: break
#release the video to avoid memory leaks, and close the window
cap.release()
cv2.destroyAllWindows()
#remove unused parts of the list
positions = positions[:frame_number]
print 'finished tracking'
#write data
import csv
with open(output_path, 'w') as csvfile:
fieldnames = ['x_position', 'y_position']
writer = csv.DictWriter(csvfile, fieldnames = fieldnames)
writer.writeheader()
for position in positions:
x, y = position[0], position[1]
writer.writerow({'x_position': x, 'y_position': y})
print 'finished writing data'
print output_path
And I get this Error
finished tracking
Traceback (most recent call last):
File "C:\Users\Tyson\Desktop\Code\Tracker.py", line 90, in <module>
with open(output_path, 'w') as csvfile:
IOError: [Errno 13] Permission denied: 'C:\\Python27'
I have tried fixing it, but nothing seems to work. Is there a native folder I could save it to that is writable? Or how can I give permission?
Thanks in advance!
The line output_path = ('C:\\Python27') later causes open(output_path, 'w') to fail. Set output_path to something else.
I am novice in Open CV and Python, I was trying to run the following code, but it was showing an error! which I could not figure out.
import cv2
import numpy as np
import util as ut
import svm_train as st
import re
model=st.trainSVM(17)
#create and train SVM model each time coz bug in opencv 3.1.0 svm.load()
https://github.com/Itseez/opencv/issues/4969
cam=int(raw_input("Enter Camera number: "))
cap=cv2.VideoCapture(cam)
font = cv2.FONT_HERSHEY_SIMPLEX
def nothing(x) :
pass
text= " "
temp=0
previouslabel=None
previousText=" "
label = None
while(cap.isOpened()):
_,img=cap.read()
cv2.rectangle(img,(900,100),(1300,500),(255,0,0),3) # bounding box which
captures ASL sign to be detected by the system
img1=img[100:500,900:1300]
img_ycrcb = cv2.cvtColor(img1, cv2.COLOR_BGR2YCR_CB)
blur = cv2.GaussianBlur(img_ycrcb,(11,11),0)
skin_ycrcb_min = np.array((0, 138, 67))
skin_ycrcb_max = np.array((255, 173, 133))
mask = cv2.inRange(blur, skin_ycrcb_min, skin_ycrcb_max) # detecting
the hand in the bounding box using skin detection
contours,hierarchy = cv2.findContours(mask.copy(),cv2.RETR_EXTERNAL, 2)
cnt=ut.getMaxContour(contours,4000) # using contours to capture the skin filtered image of the hand
if cnt!=None:
gesture,label=ut.getGestureImg(cnt,img1,mask,model) # passing the trained model for prediction and fetching the result
if(label!=None):
if(temp==0):
previouslabel=label
if previouslabel==label :
previouslabel=label
temp+=1
else :
temp=0
if(temp==40):
if(label=='P'):
label=" "
text= text + label
if(label=='Q'):
words = re.split(" +",text)
words.pop()
text = " ".join(words)
#text=previousText
print text
cv2.imshow('PredictedGesture',gesture) # showing the
best match or prediction
cv2.putText(img,label,(50,150), font,8,(0,125,155),2) # displaying the predicted letter on the main screen
cv2.putText(img,text,(50,450), font,3,(0,0,255),2)
cv2.imshow('Frame',img)
cv2.imshow('Mask',mask)
k = 0xFF & cv2.waitKey(10)
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
The error is as follows:
OpenCV Error: The sizes of input arguments do not match(The lower boundary is neither an array of the same size and same type as src,nor a scalar) in cv::inrange, file C : \build\2_4_winpack-bindings-win64-vc14-static\opencv\modules\core\src\arithm.cpp,line 2711
Traceback (most recent call last):
File "C:\Users\satwik\Downloads\asl\asl\asl.py",line 29 in <module>
mask = cv2.inRange(blur, skin_ycrcb_min, skin_ycrcb_max) # detecting
the hand in the bounding box using skin detection
cv2.error:C :\build\2_4_winpack-bindings-win64-vc14-static\opencv\modules\core\src\arithm.cpp,line 2711:error:(-209) The lower
boundary is neither an array of the same size and same type as src,nor a
scalar in function cv:inrange.
The screenshot of the error is here.
The project I was trying to run can be found here.
I would be thankful if you could help me fix the error.
These lines are wrong:
skin_ycrcb_min = np.array((0, 138, 67))
skin_ycrcb_max = np.array((255, 173, 133))
You have to use scalar (tuple), not array:
skin_ycrcb_min = (0, 138, 67)
skin_ycrcb_max = (255, 173, 133)
I am trying to run the following code on my Raspberry Pi, but it is giving me this error:
Traceback (most recent call last):
File "video_capture_thresh.py", line 59, in
main ()
File "video_capture_thresh.py", line 11, in main
crop = frame[180:320, 0:638]
TypeError: 'NoneType' object has no attribute 'getitem
import numpy as np
import cv2
#cap=cv2.VideoCapture(0)
cap = cv2.VideoCapture(1)
def main():
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
crop = frame[180:320, 0:638]
crop2=cv2.cvtColor(crop,cv2.COLOR_BGR2GRAY)
th,crop2 = cv2.threshold(crop2,0,255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
previous = cv2.GaussianBlur(crop2, (5,5),0)
contours, hierarchy = cv2.findContours(crop2,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
cv2.rectangle(previous,(0,0),(638,140),(0,255,0),5)
i=0
for cnt in contours:
moments = cv2.moments(cnt) # Calculate moments
if moments['m00']!=0:
cx = int(moments['m10']/moments['m00']) # cx = M10/M00
cy = int(moments['m01']/moments['m00']) # cy = M01/M00
moment_area = moments['m00'] # Contour area from moment
contour_area = cv2.contourArea(cnt) # Contour area using in_built function
perimeter = cv2.arcLength(cnt,True)
cv2.drawContours(previous, [cnt], 0, (0,255,0), 3)
px = previous[cy,cx]
if px == 255 :
i=i+1
cv2.circle(previous,(cx,cy),5,(0,0,255),-1)
cv2.imshow("Previous",previous)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
main ()
cap.release()
cv2.destroyAllWindows()
Try adding a check to make sure you actually read in OK before you do your processing
ret, frame = cap.read()
if ret==True:
crop = frame[180:320, 0:638]
The method read
This is the most convenient method for reading video files or capturing data from decode and return the just grabbed frame. If no frames has been grabbed (camera has been disconnected, or there are no more frames in video file), the methods return false and the functions return NULL pointer.
Check if the image is captured from camera well.
What kind of camera are you using?
Note that cv2.VideoCapture does not work with Raspi module camera, it just works with usb webcam.