【发布时间】:2018-11-16 12:11:22
【问题描述】:
我有一个 python 文本,我想把它放在 kivy 的一个类中。然后我想将这个类用作一个函数并从另一个类中调用它。我应该如何定义类?括号class FaceGenerator()应该写什么?
class FaceGenerator():
# open the camera and capture video
cam = cv2.VideoCapture(0)
face_detector =
cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# Asking the user for an ID and Name
ID = raw_input('Please insert your ID number ')
Name= raw_input('Please insert your Name ')
sample_number = 0 # a counter that counts the number of pictures for
each person in the database
# detecting the face and draw rectangle on it
while (True):
retval,image = cam.read() # reading image from cam
print np.shape(image)
gray_image = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) # converting
image to gray image
faces = face_detector.detectMultiScale(gray_image,1.3,5)
''' detectMultiScale, detects objects of different sizes in the
input image.
the detected objects are returned as a list of rectangles
'''
for (x,y,w,h) in faces:
cv2.rectangle(image, (x,y), (x+w, y+h), (255,0,0), 2)
sample_number=sample_number+1
# saving the captured face in the facebase folder
cv2.imwrite('Trainer/User.'+ID+'.'+str(sample_number)+'.jpg',
gray_image[y:y+h,x:x+w])
# this loop drawing a rectabgle on the face while the cam is open
cv2.imshow('frame',image)
if cv2.waitKey(100) & 0xFF == ord('q'):
break
elif sample_number==20:
break
cam.release()
cv2.destroyAllWindows()
return Label(text = "Succesfully created trainning set")
【问题讨论】:
-
写入括号对象,--> 类 FaceGenerator(object)。但是你定义的并不是一个真正的类。我认为只需定义一个可以从程序 def FaceGenerator () 中的任何位置调用的函数来为您提供更好的服务:
-
我只需定义一个返回字符串的函数,然后通过遍历您的小部件树并更新标签的文本属性而不返回新标签来更新它。
-
我认为关注these trivial rules 可以让你的帖子变得更好,只需一点点工作。