def initRecorder(self): # Create the recorder codec = cv.CV_FOURCC('M', 'J', 'P', 'G') self.writer = cv.CreateVideoWriter( datetime.now().strftime("%Y%m%d_%H%M%S") + ".wmv", codec, 8, cv.GetSize(self.frame), 1) # FPS set to 30 because it seems to be the fps of my cam but should be ajusted to your needs self.countdownFont = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 3, 3, 0, 5, 8) # Creates a font self.timeFont = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 1, 1, 0, 2, 8) # Creates a font
def initRecorder(self): #Create the recorder codec = cv2.CV_FOURCC('M', 'J', 'P', 'G') #('W', 'M', 'V', '2') self.writer = cv2.CreateVideoWriter( datetime.now().strftime("%b-%d_%H_%M_%S") + ".wmv", codec, 5, cv2.GetSize(self.frame), 1) #FPS set to 5 because it seems to be the fps of my cam but should be ajusted to your needs self.font = cv2.InitFont(cv2.CV_FONT_HERSHEY_SIMPLEX, 1, 1, 0, 2, 8) #Creates a font
def initRecorder(self): #Create the recorder codec = cv.CV_FOURCC('D', 'I', 'V', 'X') #codec = cv.CV_FOURCC("D", "I", "B", " ") self.writer = cv.CreateVideoWriter( datetime.now().strftime("%b-%d_%H:%M:%S") + ".avi", codec, 15, cv.GetSize(self.frame), 1) #FPS set at 15 because it seems to be the fps of my cam but should be ajusted to your needs self.font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 1, 1, 0, 2, 8) #Creates a font
def run(self): #initiate font font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 1, 1, 0, 3, 8) # instantiate images hsv_img = cv.CreateImage(cv.GetSize(cv.QueryFrame(self.capture)), 8, 3) threshold_img1 = cv.CreateImage(cv.GetSize(hsv_img), 8, 1) threshold_img1a = cv.CreateImage(cv.GetSize(hsv_img), 8, 1) threshold_img2 = cv.CreateImage(cv.GetSize(hsv_img), 8, 1) i = 0 writer = cv.CreateVideoWriter('angle_tracking.avi', cv.CV_FOURCC('M', 'J', 'P', 'G'), 30, cv.GetSize(hsv_img), 1) while True: # capture the image from the cam img = cv.QueryFrame(self.capture) # convert the image to HSV cv.CvtColor(img, hsv_img, cv.CV_BGR2HSV) # threshold the image to isolate two colors cv.InRangeS(hsv_img, (165, 145, 100), (250, 210, 160), threshold_img1) # red cv.InRangeS(hsv_img, (0, 145, 100), (10, 210, 160), threshold_img1a) # red again cv.Add(threshold_img1, threshold_img1a, threshold_img1) # this is combining the two limits for red cv.InRangeS(hsv_img, (105, 180, 40), (120, 260, 100), threshold_img2) # blue # determine the moments of the two objects threshold_img1 = cv.GetMat(threshold_img1) threshold_img2 = cv.GetMat(threshold_img2) moments1 = cv.Moments(threshold_img1, 0) moments2 = cv.Moments(threshold_img2, 0) area1 = cv.GetCentralMoment(moments1, 0, 0) area2 = cv.GetCentralMoment(moments2, 0, 0) # initialize x and y x1, y1, x2, y2 = (1, 2, 3, 4) coord_list = [x1, y1, x2, y2] for x in coord_list: x = 0 # there can be noise in the video so ignore objects with small areas if (area1 > 200000): # x and y coordinates of the center of the object is found by dividing the 1,0 and 0,1 moments by the area x1 = int(cv.GetSpatialMoment(moments1, 1, 0) / area1) y1 = int(cv.GetSpatialMoment(moments1, 0, 1) / area1) # draw circle cv.Circle(img, (x1, y1), 2, (0, 255, 0), 20) # write x and y position cv.PutText(img, str(x1) +', '+str(y1), (x1, y1 + 20), font, 255) # Draw the text if (area2 > 100000): # x and y coordinates of the center of the object is found by dividing the 1,0 and 0,1 moments by the area x2 = int(cv.GetSpatialMoment(moments2, 1, 0) / area2) y2 = int(cv.GetSpatialMoment(moments2, 0, 1) / area2) # draw circle cv.Circle(img, (x2, y2), 2, (0, 255, 0), 20) cv.PutText(img, str(x2) +', '+str(y2), (x2, y2 + 20), font, 255) # Draw the text cv.Line(img, (x1, y1), (x2, y2), (0, 255, 0), 4, cv.CV_AA) # draw line and angle cv.Line(img, (x1, y1), (cv.GetSize(img)[0], y1), (100, 100, 100, 100), 4, cv.CV_AA) x1 = float(x1) y1 = float(y1) x2 = float(x2) y2 = float(y2) angle = int(math.atan((y1 - y2) / (x2 - x1)) * 180 / math.pi) cv.PutText(img, str(angle), (int(x1) + 50, (int(y2) + int(y1)) / 2), font, 255) # cv.WriteFrame(writer,img) # display frames to users cv.ShowImage('Target', img) cv.ShowImage('Threshold1', threshold_img1) cv.ShowImage('Threshold2', threshold_img2) cv.ShowImage('hsv', hsv_img) # Listen for ESC or ENTER key c = cv.WaitKey(7) % 0x100 if c == 27 or c == 10: break cv.DestroyAllWindows()
import cv2 import numpy as np img = cv2.imread('Play_Ex/Play Capture 2.PNG') lowerBound = np.array([50, 15, 65]) upperBound = np.array([60, 60, 100]) kernelOpen = np.ones((5, 5)) kernelClose = np.ones((20, 20)) font = cv2.InitFont(cv2.CV_FONT_HERSHEY_SIMPLEX, 2, 0.5, 0, 3, 1) img = cv2.imread('Play_Ex/Play Capture 2.PNG') img = cv2.resize(img, (340, 220)) #convert BGR to HSV imgHSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) # create the Mask mask = cv2.inRange(imgHSV, lowerBound, upperBound) cv2.imshow("mask", mask) cv2.imshow("cam", img) cv2.waitKey(10) ''' #morphology maskOpen=cv2.morphologyEx(mask,cv2.MORPH_OPEN,kernelOpen) maskClose=cv2.morphologyEx(maskOpen,cv2.MORPH_CLOSE,kernelClose) maskFinal=maskClose conts,h=cv2.findContours(maskFinal.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
import cv2 import numpy as np faceDetect = cv2.CascadeClassifier("haarcascade_frontalface_default.xml") cam = cv2.VideoCapture(0) rec = cv2.face.LBPHFaceRecognizer_create() rec.read("recognizer/trainingData.yml") id = 0 font = cv2.InitFont(cv2.cv.CV_FONT_HERSHEY_COMPLEX_SMALL, 5, 1, 0, 4) while (True): ret, img = cam.read() gray = cv2.cvtColor(img, cv2.COLOR.BGR2GRAY) faces = faceDetect.detectMultiScale(gray, 1.3, 5) for (x, y, w, h) in faces: cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2) id, conf = rec.predict(gray[y:y + h, x:x + w]) if (id == 1): id = "Teyfik" elif (id == 2): id = "İbrahim" cv2.imshow("Face", img) if (cv2.waitKey(1) == ord('q')): break cam.release() cv2.destroyAllWindows()
def _get_font(self, size=1, weight=1, italic=0): return cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, size, size, italic, weight)