def draw_window(frame):
    # setup initial location of window
    r,h,c,w = 250,90,400,125  # simply hardcoded the values
    track_window = (c,r,w,h)    

    # set up the ROI for tracking
    roi = frame[r:r+h, c:c+w]
    hsv_roi =  cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
    mask = cv2.inRange(hsv_roi, np.array((0., 60.,32.)), np.array((180.,255.,255.)))
    roi_hist = cv2.calcHist([hsv_roi],[0],mask,[180],[0,180])
    cv2.normalize(roi_hist,roi_hist,0,255,cv2.NORM_MINMAX)    

    # Setup the termination criteria, either 10 iteration or move by atleast 1 pt
    term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 )

    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    dst = cv2.calcBackProject([hsv],[0],roi_hist,[0,180],1)

    # apply meanshift to get the new location
    ret, track_window = cv2.CamShift(dst, track_window, term_crit)
    # Draw it on image
    pts = cv2.boxPoints(ret)
    pts = np.int0(pts)
    img2 = cv2.polylines(frame,[pts],True, 255,2)
    io.imshow(img2)
示例#2
0
def make_sets():
    training_data = []
    training_labels = []
    prediction_data = []
    prediction_labels = []
    for emotion in emotions:
        training, prediction = get_files(emotion)
        # Append data to training and prediction list, and generate labels 0-7
        for item in training:
            image = cv2.imread(item)  # open image
            gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)  # convert to grayscale
            clahe_image = clahe.apply(gray)
            landmarks_vectorised = get_landmarks(clahe_image)
            if landmarks_vectorised == "error":
                pass
            else:
                training_data.append(landmarks_vectorised)  # append image array to training data list
                training_labels.append(emotions.index(emotion))

        for item in prediction:
            image = cv2.imread(item)
            gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
            clahe_image = clahe.apply(gray)
            landmarks_vectorised = get_landmarks(clahe_image)
            if landmarks_vectorised == "error":
                pass
            else:
                prediction_data.append(landmarks_vectorised)
                prediction_labels.append(emotions.index(emotion))

    return training_data, training_labels, prediction_data, prediction_labels
def Color_Features_Extract(img_folder):
    print "Color_Features_Extract Start"
    starttime = datetime.datetime.now()

    back = np.array([255,128,128])

    image_num = len(os.listdir(seg_img_folder))

    Color_Features = []

    for index, image_name in enumerate(os.listdir(img_folder)):
        image_path = img_folder + str("/") +image_name
        image = cv2.cvtColor(cv2.imread(image_path), cv2.COLOR_BGR2LAB)
        rows, columns, lab = image.shape

        # Make densely-sampling color features
        pixel_index = 0

        for x in range(rows):
            for y in range(columns):
                if pixel_index % 9 == 0 and np.array_equal(image[x][y],back) == False:
                    Color_Features.append(image[x][y].tolist())
                pixel_index += 1

    # Get CodeBook of Color_Features
    Color_Features = np.float32(Color_Features)

    # Define criteria = ( type, max_iter = 10 , epsilon = 1.0 )
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)

    # Set flags (Just to avoid line break in the code)
    flags = cv2.KMEANS_RANDOM_CENTERS

    # Apply KMeans
    compactness,labels,centers = cv2.kmeans(Color_Features,800,None,criteria,10,flags)

    Image_Color_Features = [[0 for x in range(800)] for y in range(image_num)]

    color_index = 0

    for image_index, image_name in enumerate(os.listdir(img_folder)):
        image_path = img_folder + str("/") +image_name
        image = cv2.cvtColor(cv2.imread(image_path), cv2.COLOR_BGR2LAB)
        rows, columns, lab = image.shape

        pixel_index = 0

        for x in range(rows):
            for y in range(columns):
                if pixel_index % 9 == 0 and np.array_equal(image[x][y],back) == False:
                    Image_Color_Features[image_index][labels[color_index]] += 1
                    color_index += 1
                pixel_index += 1
        print image_name

    endtime = datetime.datetime.now()
    print "Time: " + str((endtime - starttime).seconds) + "s"
    print "Color_Features_Extract End"

    return Image_Color_Features
示例#4
0
def mix_img(img1, img2):
    import random

    #width = max(img1.shape[0], img2.shape[0])
    #height = max(img1.shape[1], img2.shape[1])
    height = img1.shape[0] + img2.shape[0]
    width = img1.shape[1] + img2.shape[1]
    new_img = np.array([[[0,0,0]]*width]*height, dtype='uint8')

    alpha1 = img1[:,:,3]  # アルファチャンネルだけ抜き出す。
    alpha2 = img2[:,:,3]  # アルファチャンネルだけ抜き出す。
    mask = cv2.cvtColor(alpha2, cv2.COLOR_GRAY2BGR)  # 4色分に増やす。
    mask = mask / 255.0  # 0-255だと使い勝手が悪いので、0.0-1.0に変更。

    img2 = img2[:,:,:3]  # アルファチャンネルは取り出しちゃったのでもういらない。

    y1 = random.randint(0, height-img1.shape[0])
    x1 = random.randint(0, width -img1.shape[1])
    y2 = random.randint(0, height-img2.shape[0])
    x2 = random.randint(0, width -img2.shape[1])

    new_img[y1:y1+img1.shape[0], x1:x1+img1.shape[1]] = img1[:,:,:3]

    new_img[y2:y2+img2.shape[0], x2:x2+img2.shape[1]] = (new_img[y2:y2+img2.shape[0], x2:x2+img2.shape[1]] * (1 - mask)).astype('uint8')  # 透過率に応じて元の画像を暗くする。
    #new_img[y2:y2+img2.shape[0], x2:x2+img2.shape[1]] = (new_img[y2:y2+img2.shape[0], x2:x2+img2.shape[1]]).astype('uint8')  # 透過率に応じて元の画像を暗くする。
    new_img[y2:y2+img2.shape[0], x2:x2+img2.shape[1]] += (img2 * mask).astype('uint8')  # 貼り付ける方の画像に透過率をかけて加算。

    new_img = cv2.cvtColor(new_img, cv2.COLOR_BGR2BGRA)  # 4色分に増やす。
    new_img[:, :, 3] = 0
    new_img[y1:y1+img1.shape[0], x1:x1+img1.shape[1],3] = (new_img[y1:y1+img1.shape[0], x1:x1+img1.shape[1],3] + alpha1).astype('uint8')
    new_img[y2:y2+img2.shape[0], x2:x2+img2.shape[1],3] = (new_img[y2:y2+img2.shape[0], x2:x2+img2.shape[1],3] + alpha2).astype('uint8')

    return new_img
示例#5
0
def cv2pil(cv2_img):
    if len(cv2_img.shape) == 2 or cv2_img.shape[2]==1:
        cv2_img = cv2.cvtColor(cv2_img, cv2.COLOR_GRAY2RGB)
    else:
        cv2_img = cv2.cvtColor(cv2_img, cv2.COLOR_BGR2RGB)
    pil_img = Image.fromarray(cv2_img.astype('uint8'))
    return pil_img
示例#6
0
def extractDescriptor(fileName,descriptor,space,channel):
    descriptorName = "../temp/faces/" + fileName[19:-4] + "-" + descriptor + "-descriptor.txt"
    nameSpace = ""
    nname = fileName

    newName = nname[:-3] + "ppm"
    sourceImg = cv2.imread(fileName)
    if space == 0:
        destImg = cv2.cvtColor(sourceImg, cv2.COLOR_BGR2HSV)
    elif space == 1:
        destImg = cv2.cvtColor(sourceImg, cv2.COLOR_BGR2RGB)
    elif space == 2:
        destImg = cv2.cvtColor(sourceImg, cv2.COLOR_BGR2YCR_CB)
    elif space == 4:
        destImg = cv2.cvtColor(sourceImg, cv2.COLOR_BGR2LAB)
    else:
        destImg = sourceImg
    cv2.imwrite(nname,destImg)

    command = "convert " + nname + " " + newName
    os.system(command)
    command = "rm " + fileName
    os.system(command)

    upperDesc = descriptor.upper()
    if (upperDesc == "ACC") or (upperDesc == "BIC") or (upperDesc == "LCH") or (upperDesc == "CCV"):
        command = "../descriptors/" + descriptor + "/source/bin/./" + descriptor + "_extraction " + newName + " " + descriptorName
    else:
        command = "../descriptors/" + descriptor + "/source/bin/./" + descriptor + "_extraction " + newName + " " + descriptorName + " " + str(channel)
    os.system(command)
def process_directory(args, logo_path, dir_list, denoise, look_ahead):
    """This function processes a given directory and, for each .tif file
    inside, applies the logo recognition algorithm.
    """
    for idx, file_name in enumerate(dir_list):
        if file_name.lower().endswith('.tif'):
            print file_name
            numRectangles = 0
            img = cv2.imread(logo_path + file_name, cv2.CV_LOAD_IMAGE_GRAYSCALE)
            # Extract a color image to paint in red the detected regions
            color_img_regions = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
            color_img_limit   = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
            color_img_blanks  = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
            # Denoise
            if denoise:
                img_enhanced = enhance_image(img)
                # Generate the seeds from where the logo detection starts
                seeds = calculate_seeds(img_enhanced)
                regions = expand_feature_rectangle(img, img_enhanced, img_enhanced, seeds, numRectangles, color_img_blanks, color_img_limit, color_img_regions, file_name, look_ahead)
            else:
                # Generate the seeds from where the logo detection starts
                seeds = calculate_seeds(img)
                regions = expand_feature_rectangle(img, img, img, seeds, numRectangles, color_img_blanks, color_img_limit, color_img_regions, file_name, look_ahead)

            # Expand feature rectangles around the seeds

            # Write the annotated image to the output folder
            cv2.imwrite(args.output_path + file_name, color_img_regions)
            cv2.imwrite(args.output_path_limit + file_name, color_img_limit)
            cv2.imwrite(args.output_path_blanks + file_name, color_img_blanks)
            # Write each region in its folder
            stripped_name = os.path.splitext(file_name)[0]
            os.mkdir(args.output_path_regions + stripped_name)
            for idx, region in enumerate(regions):
                cv2.imwrite(args.output_path_regions + stripped_name + "/" + str(idx) + "_" + file_name, region)
示例#8
0
def hue(img):
    if random.randrange(2):
        img = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
        img[:,:,0] = (img[:,:,0].astype(int) + random.randint(-18,18))%100
        return cv2.cvtColor(img, cv2.COLOR_HSV2BGR)
    else:
        return img
示例#9
0
文件: camtrap.py 项目: noelbk/camtrap
    def capture_frame_timer_handler(self, event=None):
        ret, frame = self.camera.read()
        if not ret:
            return

        try:
            self.writer.write(frame)
            
            height, width, byteValue = frame.shape
            byteValue = byteValue * width
            cv2.cvtColor(frame, cv2.COLOR_BGR2RGB, frame)
            image = QtGui.QImage(frame, width, height, byteValue, QtGui.QImage.Format_RGB888)
            pix = QtGui.QPixmap(image)
            self.pix = pix

            # add caption
            painter = QtGui.QPainter(pix)
            painter.setFont(self.font)
            painter.setPen(QtGui.QColor('yellow'))
            painter.setBrush(QtGui.QColor('yellow'))
            rect = QtCore.QRect(0, 0, self.capture_width, self.capture_height)
            painter.drawText(rect, QtCore.Qt.AlignTop + QtCore.Qt.AlignHCenter, self.caption_top)
            painter.drawText(rect, QtCore.Qt.AlignBottom + QtCore.Qt.AlignHCenter, self.caption_bottom)
            painter.end()
            
            for label in self.labels:
                label.setPixmap(self.pix)

            self.clear_timer.start()

        except Exception as e:
            print "WARNING: couldn't save video: %s" % e
示例#10
0
def analyzeImage(imgName):
    global hist_fullLABF
    global hist_fullHSVF
    global hist_fullRGBF
    global hist_fullLABR 
    global hist_fullRGBR 
    global hist_fullHSVR 
    print imgName    
    loadImage (imgName+'.jpg')
    loadMask(imgName+'.png')
    imgHeight=img.shape[0]
    imgWidth=img.shape[1]
    print "The image width is %d." % imgWidth
    print "The image height is %d." % imgHeight
    print "it has %d channels" % img.shape[2]
    if(img.shape[2]<3):
                    print "not enough channel to work on, try RGB images"
                    sys.exit()

    hist_fullLABF += cv2.calcHist([cv2.cvtColor(img, cv2.COLOR_BGR2LAB)],[1,2],mask,[32,32],[0,256,0,256])*100000/(imgWidth*imgHeight)
    hist_fullRGBF += cv2.calcHist([img],[1,2],mask,[32,32],[0,256,0,256])*100000/(imgWidth*imgHeight)
    hist_fullHSVF += cv2.calcHist([cv2.cvtColor(img, cv2.COLOR_BGR2HSV)],[0,1],mask,[32,32],[0,256,0,256])*100000/(imgWidth*imgHeight)

    hist_fullLABR += cv2.calcHist([cv2.cvtColor(img, cv2.COLOR_BGR2LAB)],[1,2],255-mask,[32,32],[0,256,0,256])*100000/(imgWidth*imgHeight)
    hist_fullRGBR += cv2.calcHist([img],[1,2],255-mask,[32,32],[0,256,0,256])*100000/(imgWidth*imgHeight)
    hist_fullHSVR += cv2.calcHist([cv2.cvtColor(img, cv2.COLOR_BGR2HSV)],[0,1],255-mask,[32,32],[0,256,0,256])*100000/(imgWidth*imgHeight)
示例#11
0
def mask(img):
  biggest =None
  max_area = 0
  grey = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
  #blk = cv2.bitwise_not(grey)
  kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3))
  res = cv2.morphologyEx(grey,cv2.MORPH_OPEN,kernel)
  ret,thresh = cv2.threshold(grey,127,255,0)
  contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
  dest = np.zeros(thresh.shape, np.uint8)
  print contours[::1]
  print len(contours)
  print hierarchy
  for cnt in contours[::1]:
    rect  = cv2.minAreaRect(cnt)
    points = cv2.cv.BoxPoints(rect)
    points  = np.int0(np.around(points))
    #cv2.drawContours(dest, [cnt],0,(0,255,0),2)
    #cv2.polylines(dest, [points], True,( 255,255,255), 2 )
    cv2.fillPoly(orig, [cnt], (100,20,90), 4)
    cv2.fillPoly(dest, [cnt], (255,255,255), 4)

    x = cv2.cvtColor(dest,cv2.COLOR_GRAY2RGB)
    cv2.imshow('contour-highlighted image.jpg', x)
    cv2.imwrite("../../images/bound.jpg", x)

    cv2.imshow('masked image', orig)
 def process(self):
     img1 = cv2.cvtColor(self.imgcv1, cv2.COLOR_BGR2GRAY)  #queryimage # left image
     img2 = cv2.cvtColor(self.imgcv2, cv2.COLOR_BGR2GRAY)  #trainimage # right image
     # find the keypoints and descriptors with SIFT
     kp1, des1 = self.detector.detectAndCompute(img1,None)
     kp2, des2 = self.detector.detectAndCompute(img2,None)      
     matches = self.flann.knnMatch(des1,des2,k=2)       
     pts1 = []
     pts2 = []        
     # ratio test as per Lowe's paper
     for i,(m,n) in enumerate(matches):
         if m.distance < 0.8*n.distance:
             pts2.append(kp2[m.trainIdx].pt)
             pts1.append(kp1[m.queryIdx].pt)
     pts1 = np.float32(pts1)
     pts2 = np.float32(pts2)
     M, mask = cv2.findHomography(pts1, pts2, cv2.RANSAC,5.0)
     h,w = img1.shape
     h/=2
     w/=2
     pts = np.float32([ [0,0],[0,h-1],[w-1,h-1],[w-1,0] ]).reshape(-1,1,2)
     dst = cv2.perspectiveTransform(pts,M)
     cv2.polylines(img2,[np.int32(dst)],True,255,3)
     #We select only inlier points
     pts1 = pts1[mask.ravel()==1]
     pts2 = pts2[mask.ravel()==1]
     self.data=(M,pts1,pts2)
     pts1i=np.int32(pts1)
     pts2i=np.int32(pts2)
     return drawpoints(img1,img2,pts1i,pts2i)
 def process(self,imgcv):
     gray = cv2.cvtColor(imgcv, cv2.COLOR_BGR2GRAY)
     sobelx64f=cv2.Laplacian(gray,cv2.CV_64F)
     abs_sobel64f = np.absolute(sobelx64f)
     sobel_8u = np.uint8(abs_sobel64f)
     ret=cv2.cvtColor(sobel_8u,cv2.COLOR_GRAY2BGR)
     return ret
示例#14
0
 def scan_image_different_threshs(self, cv_image):
     '''Scan image using multiple thresholds if first try fails. Return list of data found in image.'''
     scan_try = 0
     qr_data = []
     while True:
         if scan_try == 0:
             image_to_scan = cv_image # use original image
         elif scan_try == 1:
             cv_gray_image = cv2.cvtColor(cv_image, cv2.COLOR_BGR2GRAY)
             cv_thresh_image = cv2.adaptiveThreshold(cv_gray_image, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 101, 2)
             image_to_scan = cv2.cvtColor(cv_thresh_image, cv2.COLOR_GRAY2BGR)
         elif scan_try == 2:
             cv_gray_image = cv2.cvtColor(cv_image, cv2.COLOR_BGR2GRAY)
             _, cv_thresh_image = cv2.threshold(cv_gray_image, 150, 255, 0)
             image_to_scan = cv2.cvtColor(cv_thresh_image, cv2.COLOR_GRAY2BGR)
         else:
             break # nothing else to try.
         
         qr_data = self.scan_image(image_to_scan)
         
         if len(qr_data) > 0:
             break # found code data in image so don't need to keep trying
         
         scan_try += 1
         
     # Notify if had to use a backup thresholding and had success.
     if scan_try > 0 and len(qr_data) > 0:
         print 'success on scan try {0}'.format(scan_try)
         
     return qr_data
示例#15
0
	def equalize_color(self):
		img1 = cv2.cvtColor(self.data, cv2.COLOR_RGB2HSV)
		dst = img1[:, :, 2]
		val = Equalizer(dst)
		eq = val.equalize_gray() 
		img1[:, :, 2] = eq
		return cv2.cvtColor(img1, cv2.COLOR_HSV2RGB)
示例#16
0
    def vj(self):
        gray = cv2.cvtColor(self.imageToDetect, cv2.COLOR_BGR2GRAY)
        fd = fdlib.FaceDetector(
            './faceDetector/haarcascade_frontalface_default.xml')
        gray = cv2.cvtColor(self.imageToDetect, cv2.COLOR_BGR2GRAY)
        faces = fd.detect(gray, scaleFactor=1.08, minNeighbors=5,
                          minSize=(30, 30))
        print(faces)
        for (x, y, w, h) in faces:
            x = (x+10)
            y = (y+10)
            w = (w-15)
            h = (h-15)
            print("face")
            cv2.rectangle(
                self.imageToDetect, (x, y), (x+w, y+h), (255, 0, 0), 2)
            roi_gray = gray[y:y+h, x:x+w]
            roi_color = self.imageToDetect[y:y+h, x:x+w]
            eyes = self.eye_cascade.detectMultiScale(roi_gray)
            cv2.rectangle(
                self.imageToDetect, (x, y), (x+w, y+h), (255, 0, 0), 2)

        if(self.type == 'image'):
            cv2.imshow('Image', self.imageToDetect)
            cv2.waitKey(0)
        else:
            return faces
示例#17
0
    def queryFrame(self):

        #        cvBGRImg = cv2.imread(self.image_Data_files[self.pos])
        cvBGRImg = processImage(self.image_Data_files[self.pos])
        cvBGRImg2 = self.edgedetection.computeEdges(cvBGRImg)
        cvBGRImg3b = self.bev.computeBev(cvBGRImg)
        cvBGRImg2a = cv2.cvtColor(cvBGRImg2, cv2.cv.CV_GRAY2BGR)
        cvBGRImg3 = self.bev.computeBev(cvBGRImg2a)
        cvBGRImg3a = cv2.cvtColor(cvBGRImg3, cv2.cv.CV_BGR2GRAY)
        rev, cvBGRImg3a = cv2.threshold(cvBGRImg3a, 200, 255, cv2.THRESH_BINARY)
        # cvBGRImg4 = self.linefitter.findLine(cvBGRImg3a)
        cvBGRImg5 = self.polygonfitter.findPolygon(cvBGRImg3a, cvBGRImg3b.copy())
        cvBGRImg6 = self.bev.computePers(cvBGRImg5)

        self.qpm4 = convertIpl(cvBGRImg6)
        self.qpm3 = convertIplG(cvBGRImg3a)
        self.qpm2 = convertIplG(cvBGRImg2)
        self.qpm = convertIpl(cvBGRImg)

        if len(self.image_Data_files) > self.pos + 1:
            self.pos += 1
        else:
            self.pos = 0

        self.imageLabel.setPixmap(self.qpm)
        self.imageLabel2.setPixmap(self.qpm2)
        self.imageLabel3.setPixmap(self.qpm3)
        self.imageLabel4.setPixmap(self.qpm4)
示例#18
0
def cartonify_image(image):
    """
    convert an inpuy image to a cartoon-like image
    Args:
       image: input PIL image

    Returns:
        out (numpy.ndarray): A grasycale or color image of dtype uint8, with
                             the shape of image
    """

    output = np.array(image)
    x, y, c = output.shape

    # noise removal while keeping edges sharp
    for i in xrange(c):
        output[:, :, i] = cv2.bilateralFilter(output[:, :, i], 5, 50, 50)

    #edges in an image using the Canny algorithm
    edge = cv2.Canny(output, 100, 200)
    #convert image into RGB color space
    output = cv2.cvtColor(output, cv2.COLOR_RGB2HSV)

    #historygram array
    hists = []

    #Compute the histogram of a set of data.
    #H
    hist, _ = np.histogram(output[:, :, 0], bins=np.arange(180+1))
    hists.append(hist)
    #S
    hist, _ = np.histogram(output[:, :, 1], bins=np.arange(256+1))
    hists.append(hist)
    #V
    hist, _ = np.histogram(output[:, :, 2], bins=np.arange(256+1))
    hists.append(hist)

    centroids = []
    for h in hists:
        centroids.append(kmeans_histogram(h))
    print("centroids: {0}".format(centroids))

    output = output.reshape((-1, c))
    for i in xrange(c):
        channel = output[:, i]
        index = np.argmin(np.abs(channel[:, np.newaxis] - centroids[i]), axis=1)
        output[:, i] = centroids[i][index]
    output = output.reshape((x, y, c))
    output = cv2.cvtColor(output, cv2.COLOR_HSV2RGB)

    # Retrieves contours from the binary image
    # RETR_EXTERNAL: retrieves only the extreme outer contours
    # CHAIN_APPROX_NONE= stores absolutely all the contour points
    contours, _ = cv2.findContours(edge,
                                   cv2.RETR_EXTERNAL,
                                   cv2.CHAIN_APPROX_NONE)

    # Draws contours outlines
    cv2.drawContours(output, contours, -1, 0, thickness=1)
    return output
示例#19
0
def draw_match(img1, kp1,
               img2, kp2, 
               matches,
               output = None,
               matchColor = (255,0,0),
               matchesMask = None,
               *args, **kargs):
    h1 = img1.shape[0]
    w1 = img1.shape[1]
    
    h2 = img2.shape[0]
    w2 = img2.shape[1]
    
    w = w1 + w2
    h = max(h1, h2)
    
    outimg = np.full((h,w,3), 0, np.uint8)
    

    outimg[0:h1,0:w1, :] = cv2.cvtColor(img1, cv2.COLOR_GRAY2BGR)
    outimg[0:h2,w1:w1+w2, :] = cv2.cvtColor(img2, cv2.COLOR_GRAY2BGR)
    
    for i, m in enumerate(matches):
        if matchesMask is not None and matchesMask[i] == 0: continue
        i1 , i2  = m.queryIdx, m.trainIdx
        pt1, pt2 = kp1[i1].pt, kp2[i2].pt
        pt1 = ( int(pt1[0]), int(pt1[1])) 
        pt2 = ( int(w1 + pt2[0]), int(pt2[1]))
        cv2.line(outimg, pt1, pt2, matchColor)
    
    if output is not None:
        output = outimg.copy()
    
    return outimg
示例#20
0
文件: img.py 项目: ftyszyx/tools
 def getOneTemplePos(self,srcPicPath,templePicPath):
     # print(srcPicPath,templePicPath)
     img_src=cv2.imread(srcPicPath)
     img_src_gray=cv2.cvtColor(img_src, cv2.COLOR_BGR2GRAY)
     srcw,srch=img_src_gray.shape[::-1]
     print("img_src gray",srcw,srch)
     img_temple=cv2.imread(templePicPath)
     img_temple_gray=cv2.cvtColor(img_temple, cv2.COLOR_BGR2GRAY)
     templew,templeh=img_temple_gray.shape[::-1]
     print("temple gray",templew,templeh)
     # cv2.imshow('rgb',img_src)
     # cv2.imshow('gray',img_src_gray)
     # cv2.imshow('template',img_temple_gray)
     # cv2.waitKey(0)
     # cv2.destroyAllWindows()
     res = cv2.matchTemplate(img_src_gray,img_temple_gray,method) 
     min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
     print(min_val, max_val, min_loc, max_loc)
     # If the method is TM_SQDIFF or TM_SQDIFF_NORMED, take minimum
     if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]:
         top_left = min_loc
     else:
         top_left = max_loc
     bottom_right = (top_left[0] + templew, top_left[1] + templeh)
     cv2.rectangle(img_src,top_left, bottom_right, 255, 2)
     print(top_left, bottom_right)
示例#21
0
文件: img.py 项目: ftyszyx/tools
    def getMultiTemplePos(self,srcPicPath,templePicPath):
        print("srcpath",srcPicPath,"temppath",templePicPath)
        img_src=cv2.imread(srcPicPath)
        img_src_gray=cv2.cvtColor(img_src, cv2.COLOR_BGR2GRAY)
        srcw,srch=img_src_gray.shape[::-1]
        print("get pic:",srcw,srch)
        img_temple=cv2.imread(templePicPath)
        img_temple_gray=cv2.cvtColor(img_temple, cv2.COLOR_BGR2GRAY)
        templew,templeh=img_temple_gray.shape[::-1]
        res = cv2.matchTemplate(img_src_gray,img_temple_gray,cv2.TM_CCOEFF_NORMED) 
        # print("get temple",res)
        # cv2.imshow('src',img_src_gray)
        # cv2.imshow('temple',img_temple_gray)
        # cv2.waitKey(0)

        threshold = 0.7 
        loc = np.where( res >= threshold)
        print(loc)
        # zipres=zip(*loc[::-1])
        # print("zipres",zipres)
        # if len(zipres)==0:
        #     return False,None,None,None
        # else:
        #     return True,zipres[0],templew,templeh
        for pt in zip(*loc[::-1]):
            cv2.rectangle(img_src, pt, (pt[0] + templew, pt[1] + templeh),(7,249,151), 2)   
        cv2.imshow('Detected',img_src)
        cv2.waitKey(0)
        cv2.destroyAllWindows()
示例#22
0
def warmer(img, amount):

	if (amount < 0 or amount > 1):
		raise NameError('amount must be between 0 and 1')

	incr_ch_lut = create_LUT_8UC1([0, 64,      192,      256],
	                              [0, 64 + 40*amount, 192 + 45*amount, 256])
	decr_ch_lut = create_LUT_8UC1([0, 64,      192,      256],
                       	          [0, 64 - 52*amount, 192 - 85*amount, 192])

	img_rgb = cv2.imread("images/" + filename + ".jpg")
	 
	c_r, c_g, c_b = cv2.split(img_rgb)
	c_r = cv2.LUT(c_r, incr_ch_lut).astype(np.uint8)
	c_b = cv2.LUT(c_b, decr_ch_lut).astype(np.uint8)
	img_rgb = cv2.merge((c_r, c_g, c_b))

	c_b = cv2.LUT(c_b, decr_ch_lut).astype(np.uint8)
	 

	c_h, c_s, c_v = cv2.split(cv2.cvtColor(img_rgb,
	    cv2.COLOR_RGB2HSV))
	c_s = cv2.LUT(c_s, incr_ch_lut).astype(np.uint8)
	 
	img_warmer = cv2.cvtColor(cv2.merge(
	                      (c_h, c_s, c_v)),
	                       cv2.COLOR_HSV2RGB)

	return img_warmer
示例#23
0
def maskcolor(path,_hsv_mask):
	if path=='0':
		path=0;
	cap = cv2.VideoCapture(path)

	while(cap.isOpened()):
		_, frame = cap.read()
		hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

		# Threshold the HSV image to get only blue colors
		mask = cv2.inRange(hsv,_hsv_mask[0],_hsv_mask[1]);

		# Bitwise-AND mask and original image
		_res = cv2.bitwise_and(frame,frame, mask= mask)
		_gray = cv2.cvtColor(_res,cv2.COLOR_RGB2GRAY)
		_edge = cv2.adaptiveThreshold(_gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)
		cnt,hchy = cv2.findContours(_edge,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
		print hchy
		cv2.drawContours(frame, cnt, -1, (0,255,0), 3)
		#x,y,w,h = cv2.boundingRect(np.vstack(cnt))
		#cv2.rectangle(frame,(x,y),(x+w,y+h),(0,0,255),2)		
		#rect = cv2.minAreaRect(np.vstack(cnt))
		#box = cv2.cv.BoxPoints(rect)
		#box = np.int0(box)
		#cv2.drawContours(frame,[box],0,(0,0,255),2)
		cv2.imshow('a',frame)
		#cv2.imshow('b',_edge)
		#cv2.imshow('c',res)
		#plt.show()

		if cv2.waitKey(1) & 0xFF == ord('q'):
			break
	cap.release()
	cv2.destroyAllWindows()
示例#24
0
def run_whole_video(exp_folder, lims_ID):
    #initializes video pointer for video of interest based on lims ID
    file_string = get_file_string(exp_folder, lims_ID)
    video_pointer = cv2.VideoCapture(file_string)

    # import wheel data
    wheel = joblib.load('dxds2.pkl')
    first_non_nan = next(x for x in wheel if not isnan(x))
    first_index = np.where(wheel == first_non_nan)[0]
    k = first_index[0]
    imp = Imputer(missing_values='NaN', strategy='mean')
    wheel = imp.fit_transform(wheel)
    wheel = preprocessing.MinMaxScaler((-1, 1)).fit(wheel).transform(wheel)

    # self.video_pointer.set(1, 41000)
    ret, frame = video_pointer.read()

    # crops and converts frame into desired format
    frame = cv2.cvtColor(frame[160:400, 100:640], cv2.COLOR_BGR2GRAY)

    prvs = frame
    nex = frame

    # initialize vectors to keep track of data
    count = 0
    mod = 0
    opticals = []
    angles = []
    frames = []

    # length of movie
    limit = int(video_pointer.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT))


    # create hdf file
    hf = h5py.File('data_' + str(lims_ID) + '.h5', 'w')
    g = hf.create_group('feature space')
    vector = np.zeros((limit, 4321))
    table = g.create_dataset('features', data = vector, shape =(limit, 4321))


    while count <= limit:

        prvs = nex
        frames = process_input(prvs)

        ret, frame = video_pointer.read()
        nex = cv2.cvtColor(frame[160:400, 100:640], cv2.COLOR_BGR2GRAY)

        optical = optical_flow(prvs, nex)
        opticals = optical['mag']
        angles= optical['ang']
        vector_data = np.concatenate((np.reshape(wheel[k], (1)), frames, opticals, angles))

        table[count, :] = vector_data

        count += 1

        if count%1000 == 0:
            print (count)
def detect(image):
	imgLength = len(image)
	imgWidth = len(image[0])
	pointAr = [0,0,0]
	img = image
	hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
	sensitivity = 0
	lower_color = np.array([5, 120, 150])
	upper_color = np.array([30, 255, 255])
	mask = cv2.inRange(hsv, lower_color, upper_color)
	image = img.copy()
	image = cv2.bitwise_and(image, image, mask=mask)
	gray = cv2.cvtColor(image, cv2.COLOR_HSV2BGR)
	gray = cv2.cvtColor(gray, cv2.COLOR_BGR2GRAY)
	circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT,2 , 100)
	
	if circles is not None:
		circles = np.round(circles[0, :]).astype("int")
		rng = 0
		for (x, y, r) in circles:
			cv2.circle(img, (x, y), r, (0, 255, 0), 4)
			cv2.rectangle(img, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1)
                        print(r)
			pointAr = [x,y,r]
	return pointAr
示例#26
0
def crop_waffle(img):
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    greyscale = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    lower_yellow = np.array([0,50,50])
    upper_yellow = np.array([70,255,255])
    mask = cv2.inRange(hsv, np.uint8(lower_yellow), np.uint8(upper_yellow))
    kernel = np.ones((9,9),np.uint8)
    closed_mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
    masked_img = cv2.bitwise_and(greyscale,greyscale,mask = closed_mask)
    [contours,hiearchy] = cv2.findContours(masked_img,cv.CV_RETR_EXTERNAL,cv.CV_CHAIN_APPROX_SIMPLE)
    #now find the largest contour
    max_area = 0
    max_contour = None
    for c in contours:
        #we change datatypes from numpy arrays to cv arrays and back because contour area only takes cv arrays.
        c = cv.fromarray(c)
        if cv.ContourArea(c) > max_area:
            max_contour = c
            max_area = cv.ContourArea(c)
    max_contour = np.asarray(max_contour)
    shape = img.shape
    largest_blob_mask = np.zeros((shape[0],shape[1],1),np.uint8)
    cv2.fillPoly(largest_blob_mask, pts =[max_contour], color=(255,255,255))
    print_rgb_hist(img,largest_blob_mask)
    return cv2.bitwise_and(img,img, mask= largest_blob_mask)
示例#27
0
def overlayOnPart(src_image, overlay_image, posX, posY):

    # オーバレイ画像のサイズを取得
    ol_height, ol_width = overlay_image.shape[:2]

    # OpenCVの画像データをPILに変換
    # BGRAからRGBAへ変換
    src_image_RGBA = cv2.cvtColor(src_image, cv2.COLOR_BGR2RGB)
    overlay_image_RGBA = cv2.cvtColor(overlay_image, cv2.COLOR_BGRA2RGBA)
    
    # PILに変換
    src_image_PIL=Image.fromarray(src_image_RGBA)
    overlay_image_PIL=Image.fromarray(overlay_image_RGBA)

    # 合成のため、RGBAモードに変更
    src_image_PIL = src_image_PIL.convert('RGBA')
    overlay_image_PIL = overlay_image_PIL.convert('RGBA')

    # 同じ大きさの透過キャンパスを用意
    tmp = Image.new('RGBA', src_image_PIL.size, (255, 255,255, 0))
    # 用意したキャンパスに上書き
    tmp.paste(overlay_image_PIL, (posX, posY), overlay_image_PIL)
    # オリジナルとキャンパスを合成して保存
    result = Image.alpha_composite(src_image_PIL, tmp)
    
    # COLOR_RGBA2BGRA から COLOR_RGBA2BGRに変更。アルファチャンネルを含んでいるとうまく動画に出力されない。
    return  cv2.cvtColor(np.asarray(result), cv2.COLOR_RGBA2BGR)
示例#28
0
def download(person, url, bb):
    imgName = os.path.basename(url)
    rawPersonPath = os.path.join(args.raw, person)
    rawImgPath = os.path.join(rawPersonPath, imgName)
    alignedPersonPath = os.path.join(args.aligned, person)
    alignedImgPath = os.path.join(alignedPersonPath, hashlib.md5(imgName).hexdigest()+".png")

    mkdirP(rawPersonPath)
    mkdirP(alignedPersonPath)

    if not os.path.isfile(rawImgPath):
        urlF = urllib2.urlopen(url)
        with open(rawImgPath, 'wb') as f:
            f.write(urlF.read())

    if not os.path.isfile(alignedImgPath):
        bgr = cv2.imread(rawImgPath)
        if bgr is None:
            return

        rgb = cv2.cvtColor(bgr, cv2.COLOR_BGR2RGB)

        dlibBB = dlib.rectangle(*bb)
        outRgb = align.align(96, rgb,
                             bb=dlibBB,
                             landmarkIndices=landmarkIndices)

        if outRgb is not None:
            outBgr = cv2.cvtColor(outRgb, cv2.COLOR_RGB2BGR)
            cv2.imwrite(alignedImgPath, outBgr)
示例#29
0
def grabcut(rgb_chip):
    (h, w) = rgb_chip.shape[0:2]
    _mask = np.zeros((h, w), dtype=np.uint8)  # Initialize: mask
    # Set inside to cv2.GC_PR_FGD (probably forground)
    _mask[ :, :] = cv2.GC_PR_FGD
    # Set border to cv2.GC_BGD (definitely background)
    _mask[ 0, :] = cv2.GC_BGD
    _mask[-1, :] = cv2.GC_BGD
    _mask[:,  0] = cv2.GC_BGD
    _mask[:, -1] = cv2.GC_BGD
    # Grab Cut Parameters
    rect = (0, 0, w, h)
    num_iters = 5
    mode = cv2.GC_INIT_WITH_MASK
    bgd_model = np.zeros((1, 13 * 5), np.float64)
    fgd_model = np.zeros((1, 13 * 5), np.float64)
    # Grab Cut Execution
    cv2.grabCut(rgb_chip, _mask, rect, bgd_model, fgd_model, num_iters, mode=mode)
    is_forground = (_mask == cv2.GC_FGD) + (_mask == cv2.GC_PR_FGD)
    chip_mask = np.where(is_forground, 255, 0).astype('uint8')
    # Crop
    chip_mask = clean_mask(chip_mask)
    chip_mask = np.array(chip_mask, np.float) / 255.0
    # Mask value component of HSV space
    chip_hsv = cv2.cvtColor(rgb_chip, cv2.COLOR_RGB2HSV)
    chip_hsv = np.array(chip_hsv, dtype=np.float) / 255.0
    chip_hsv[:, :, 2] *= chip_mask
    chip_hsv = np.array(np.round(chip_hsv * 255.0), dtype=np.uint8)
    seg_chip = cv2.cvtColor(chip_hsv, cv2.COLOR_HSV2RGB)
    return seg_chip
示例#30
0
def contours_info(image):
    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
    contours, hierarchy = cv.findContours(binary, cv.RETR_EXTERNAL,
                                          cv.CHAIN_APPROX_SIMPLE)
    return contours
import cv2
import numpy as np
img = cv2.imread("messi5.jpg")
grey_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
template = cv2.imread("messi_face.jpg", 0)
w, h = template.shape[::-1]

res = cv2.matchTemplate(grey_img, template, cv2.TM_CCORR_NORMED )
print(res)
threshold = 0.99;
loc = np.where(res >= threshold)
print(loc)
for pt in zip(*loc[::-1]):
    cv2.rectangle(img, pt, (pt[0] + w, pt[1] + h), (0, 0, 255), 2)

cv2.imshow("img", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
示例#32
0
import cv2

cap = cv2.VideoCapture('video.avi')
car_cascade = cv2.CascadeClassifier('cars.xml')

while True:
    ret, frames = cap.read()
    gray = cv2.cvtColor(frames, cv2.COLOR_BGR2GRAY)
    cars = car_cascade.detectMultiScale(gray, 1.1, 1)

    for (x, y, w, h) in cars:
        cv2.imshow(frames, (x, y), (x+w, y+h), (0, 0, 255), 2)

    cv2.imshow('video2', frames)

    if cv2.waitKey(33) == 27:
        break

cv2.destroyAllWindows()
示例#33
0
import json
import numpy as np
import cv2
import matplotlib.pyplot as plt

np_lena_img = cv2.imread("Lenna(origin).png", cv2.IMREAD_UNCHANGED)
np_lena_img_resize = cv2.resize(np_lena_img, (32, 32), cv2.INTER_AREA)
np_lena_img_gray = cv2.cvtColor(np_lena_img_resize, cv2.COLOR_BGR2GRAY)

plt.figure(1)
plt.imshow(np_lena_img_gray, cmap='gray')

np_lena_img_dct = cv2.dct(np_lena_img_gray.astype(np.float32))
np_leana_img_dct_show = np.uint8(np_lena_img_dct)

plt.figure(2)
plt.imshow(np_leana_img_dct_show, cmap='gray')

np_leana_img_dct_low_freq = np_lena_img_dct[0:8, 0:8]
for i in range(8):
    ls_row = [
        str(np.round(e, 2)) for e in np_leana_img_dct_low_freq[i, :].tolist()
    ]
    print("\t".join(ls_row))

a = np.mean(np_leana_img_dct_low_freq)
print("a", a)
for i in range(8):
    ls_row_hash = [
        "1" if e >= a else "0"
        for e in np_leana_img_dct_low_freq[i, :].tolist()
    # Set transient motion detected as false
    transient_movement_flag = False

    # Read frame
    ret, frame = cap.read()
    text = "Unoccupied"

    # If there's an error in capturing
    if not ret:
        print("CAPTURE ERROR")
        continue

    # Resize and save a greyscale version of the image
    frame = imutils.resize(frame, width=750)
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Blur it to remove camera noise (reducing false positives)
    gray = cv2.GaussianBlur(gray, (21, 21), 0)

    # If the first frame is nothing, initialise it
    if first_frame is None: first_frame = gray

    delay_counter += 1

    # Otherwise, set the first frame to compare as the previous frame
    # But only if the counter reaches the appriopriate value
    # The delay is to allow relatively slow motions to be counted as large
    # motions if they're spread out far enough
    if delay_counter > FRAMES_TO_PERSIST:
        delay_counter = 0
示例#35
0
elect = []
for k in range(5):
    for i in range(6):
        # slice 0
        if "000" in fname:
            if k==0 and i in (0,5):
                continue
            elif k==4 and i in (0, 4, 5):
                continue
        elect.append(((65+k*30)*pix,(50+i*30)*pix))
for i in range(len(elect)):
    cv2.circle(im, (int(elect[i][1]), int(elect[i][0])), radius,color,-1)  #max(int(radius),1), color, -1)

cv2.imwrite(contourdir+fname+'_elect.jpg', im)

imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)

ret, thresh1 = cv2.threshold(imgray, threshold_value, 255, cv2.THRESH_TOZERO)
thresh = cv2.adaptiveThreshold(thresh1, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,41, 2)

contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
print("Number of contours found %s", len(contours))

blank_image = np.zeros((1024,1024,3), np.uint8)
blank_image[:,:] = (255,255,255)
blank_imageb = np.zeros((1024,1024,3), np.uint8)
blank_imageb[:,:] = (255,255,255)
contours_poly = [None]*len(contours)

print("Computing poly contours (drawing1.jpg, drawing1b.jpg)")
for i, c in enumerate(contours):
示例#36
0
def process(inpath, outpath, tolerance):
   original_image = cv2.imread(inpath)
   tolerance = int(tolerance) * 0.01

   
   width, height, channels = original_image.shape

   color_image = original_image.copy()

   blue_hist = cv2.calcHist([color_image], [0], None, [256], [0, 256])
   green_hist = cv2.calcHist([color_image], [1], None, [256], [0, 256])
   red_hist = cv2.calcHist([color_image], [2], None, [256], [0, 256])

   blue_mode = blue_hist.max()
   blue_tolerance = np.where(blue_hist == blue_mode)[0][0] * tolerance
   green_mode = green_hist.max()
   green_tolerance = np.where(green_hist == green_mode)[0][0] * tolerance
   red_mode = red_hist.max()
   red_tolerance = np.where(red_hist == red_mode)[0][0] * tolerance

   sloop_blue = calc_sloop_change(blue_hist, blue_mode, blue_tolerance)
   sloop_green = calc_sloop_change(green_hist, green_mode, green_tolerance)
   sloop_red = calc_sloop_change(red_hist, red_mode, red_tolerance)

   gray_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2GRAY)
   gray_hist = cv2.calcHist([original_image], [0], None, [256], [0, 256])

   largest_gray = gray_hist.max()
   threshold_gray = np.where(gray_hist == largest_gray)[0][0]

   #Red cells
   gray_image = cv2.adaptiveThreshold(gray_image, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 85, 4)

   x, contours, hierarchy = cv2.findContours(gray_image, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)

   c2 = [i for i in contours if cv2.boundingRect(i)[3] > 15]
   cv2.drawContours(color_image, c2, -1, (0, 0, 255), 1)

   cp = [cv2.approxPolyDP(i, 0.015 * cv2.arcLength(i, True), True) for i in c2]

   countRedCells = len(c2)

   for c in cp:
      xc, yc, wc, hc = cv2.boundingRect(c)
      cv2.rectangle(color_image, (xc, yc), (xc + wc, yc + hc), (0, 255, 0), 1)

   #Malaria cells
   gray_image = cv2.inRange(original_image, np.array([sloop_blue, sloop_green, sloop_red]), np.array([255, 255, 255]))

   x, contours, hierarchy = cv2.findContours(gray_image, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)

   c2 = [i for i in contours if cv2.boundingRect(i)[3] > 8]
   cv2.drawContours(color_image, c2, -1, (0, 0, 0), 1)

   cp = [cv2.approxPolyDP(i, 0.15 * cv2.arcLength(i, True), True) for i in c2]

   countMalaria = len(c2)

   for c in cp:
      xc, yc, wc, hc = cv2.boundingRect(c)
      cv2.rectangle(color_image, (xc, yc), (xc + wc, yc + hc), (0, 0, 0), 1)

   cv2.imwrite(outpath, color_image)
   

   #Write statistics
   with open(outpath + '.stats', mode='w') as f:
	f.write(str(countRedCells) + '\n')
	f.write(str(countMalaria) + '\n')
示例#37
0
        keepball = 0

        for im in balls:
            print im
            if len(im) == 4:
                image = cv2.imread(basedir + '/' + im[0])
                row = int(im[1])
                col = int(im[2])
                rad = float(im[3])

                if (row - window_size) < 0 or (row + window_size) > image.shape[0] or (col - window_size) < 0 or (col + window_size) > image.shape[1]:
                    print 'ignore'
                    continue

                image = cv2.cvtColor(image, cv2.COLOR_YCR_CB2BGR)
                # cv2.imshow("test", image)
                dst = cv2.remap(image, map1, map2, cv2.INTER_LINEAR)

                # np.random.randint(1, 15)

                data = dst[row - window_size: row + window_size,
                           dst.shape[1] - col - window_size:dst.shape[1] - col + window_size]
                nbball += 1
                if (nbball % 10) == 0:  # reserve some data for tests
                    cv2.imwrite('keep_%06d.png' % (nbball), data)
                    continue

                cv2.imwrite('%06d.png' % (nbball), data)

                # cv2.imshow('test box', data)
import numpy as np
import cv2

cap = cv2.VideoCapture('D:\\project\\python\\worm\\data\\20Xc.avi')

ret, frame1 = cap.read()
prvs = cv2.cvtColor(frame1,cv2.COLOR_BGR2GRAY)
hsv = np.zeros_like(frame1)
hsv[...,1] = 255

while(1):
    ret, frame2 = cap.read()
    next = cv2.cvtColor(frame2,cv2.COLOR_BGR2GRAY)

    flow = cv2.calcOpticalFlowFarneback(prvs,next, None, 0.5, 3, 15, 3, 5, 1.2, 0)

    mag, ang = cv2.cartToPolar(flow[...,0], flow[...,1])
    hsv[...,0] = ang*180/np.pi/2
    hsv[...,2] = cv2.normalize(mag,None,0,255,cv2.NORM_MINMAX)
    rgb = cv2.cvtColor(hsv,cv2.COLOR_HSV2BGR)
    rgb=cv2.resize(rgb,(400,400))
    cv2.imshow('frame2',rgb)
    k = cv2.waitKey(30) & 0xff
    if k == 27:
        break
    elif k == ord('s'):
        cv2.imwrite('opticalfb.png',frame2)
        cv2.imwrite('opticalhsv.png',rgb)
    prvs = next

cap.release()
import cv2
import numpy as np

kamera = cv2.VideoCapture(0)
while True:
    ret, frame = kamera.read()
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    altdeger_kirmizi = np.array([150, 30, 30])
    ustdeger_kirmizi = np.array([190, 255, 255])

    mask = cv2.inRange(hsv, altdeger_kirmizi, ustdeger_kirmizi)
    son_resim = cv2.bitwise_and(frame, frame, mask=mask)

    kernel = np.ones((5, 5), np.uint8)
    erosion = cv2.erode(mask, kernel, iterations=1)
    dilation = cv2.dilate(mask, kernel, iterations=1)

    closing = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
    opening = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)

    cv2.imshow('sonresim', son_resim)
    cv2.imshow('erosion', erosion)
    cv2.imshow('dilation', dilation)
    cv2.imshow('closing', closing)
    cv2.imshow('opening', opening)

    if cv2.waitKey(50) & 0xFF == ord('q'):
        break
kamera.release()
示例#40
0
 def loadImg(self, img):
     self.image = img
     self.gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
示例#41
0
# Should take this a configs...
topic = "kth/dm2518/pose"
replyTopic = "ha/camera/reply/mqtt"
mqttBroker = "mqtt.eclipse.org"

# Connect to the broker
client = MQTTImageProcess(topic, id="pose-img")
client.connect(mqttBroker)
client.subscribe(topic + "/#", 0)
client.loop_start()

while (True):
    if client.show_frame:
        nf = client.frame.copy()
        # You may need to convert the color.
        img = cv2.cvtColor(nf, cv2.COLOR_BGR2RGB)
        im_pil = Image.fromarray(img)
        d = pose_test.process_poses(im_pil)
        nf = np.asarray(im_pil)
        # convert to a openCV2 image, notice the COLOR_RGB2BGR which means that
        # the color is converted from RGB to BGR format
        nf = cv2.cvtColor(nf, cv2.COLOR_RGB2BGR)
        print("Should show frame and reply.")
        if d != []:
            if (client.type == client.FRAME_RAW):
                print("Nothing for raw")
            elif (client.type == client.FRAME_B64):
                img = b'data:image/jpeg;base64,' + base64.encodebytes(
                    cv2.imencode('.jpeg', nf)[1].tostring())
                print("IMG:" + img.decode('ascii'))
                # Add reply in topic
示例#42
0
color_max = np.array([12,255,255], np.uint8)

# Measure time for science!
past = datetime.datetime.now()

# loop until forever
while True:
    # time how long a cycle takes
    current = datetime.datetime.now()
    diff = current - past
    print "FPS:", 1.0/(diff.seconds + diff.microseconds*1E-6)
    past = current

    # capture image from camera
    rv,img = webcam.read()
    if not rv:
        print "ERROR capturing image"
        exit(1)

    # TODO: this may be overkill on the blur
    cv2.GaussianBlur(img,(21,21),0)

    # Convert to HSV before threshold
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

    # Threshold within csv
    thresh = cv2.inRange(hsv, color_min, color_max)

    # Show the result
    cv2.imshow('foo', thresh)
    pool = Pool(args.num_workers, worker, (input_q, output_q))
    video_path = '2.mp4'
    vidcap = cv2.VideoCapture(video_path)
    # video_capture = WebcamVideoStream(src=args.video_source,
    #                                   width=args.width,
    #                                   height=args.height).start()
    fps = FPS().start()
    success, img = vidcap.read()
    while success:  # fps._numFrames < 120
        #frame = video_capture.read()
        success, frame = vidcap.read()
        input_q.put(frame)

        t = time.time()

        output_rgb = cv2.cvtColor(output_q.get(), cv2.COLOR_RGB2BGR)
        cv2.imshow('Video', output_rgb)
        fps.update()

        print('[INFO] elapsed time: {:.2f}'.format(time.time() - t))

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    fps.stop()
    print('[INFO] elapsed time (total): {:.2f}'.format(fps.elapsed()))
    print('[INFO] approx. FPS: {:.2f}'.format(fps.fps()))

    pool.terminate()
    # video_capture.stop()
    cv2.destroyAllWindows()
					depth_frame = np.ascontiguousarray(depth_frame_orig)
					# depth_frame is transformed, the color map will be applied to highlight the depth info
					depth_frame = apply_colormap(depth_frame, cmap=13)
					# depth_frame is ready to be shown
					cv2.imshow("disparity", depth_frame)
			
					# Retrieve 'bgr' (opencv format) frame
					rgb_frame = in_rgb.getCvFrame()
					if args.debug_img_sizes:
						print(f'{rgb_frame.shape = } - {len(rgb_frame) = } - {type(rgb_frame) = } - {rgb_frame.size = }')
					cv2.imshow("rgb", rgb_frame)
	
					#img_grey = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
					depth_frame_th = cv2.adaptiveThreshold(depth_frame_orig, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
					cv2.imshow("disparity th", depth_frame_th)
					depth_frame_th_color = cv2.cvtColor(depth_frame_th, cv2.COLOR_GRAY2BGR)
	
					rgb_frame_resized = cv2.resize(rgb_frame, dsize=(depth_w, depth_h), interpolation=cv2.INTER_CUBIC)
					combo = (rgb_frame_resized + depth_frame_th_color) / 2
					cv2.imshow("combo", combo)
			
					if cv2.waitKey(1) == ord('q'):
						break

				cmap_counter += 1

		except KeyboardInterrupt:
			# Keyboard interrupt (Ctrl + C) detected
			pass

	print("To view the encoded data, convert the stream file (.h265) into a video file (.mp4) using a command below:")
示例#45
0
def refine_bboxes(bboxes, classes, frame, trackers):
    # Refine boxes and reinitialize trackers.
    # Boxes are refined to be as tight as possible to the object being tracked.
    # The tracker is then given the bbox which has been inflated by the original
    # scale factor, to preserve tracking quality.

    # Just in case the tracker is missing something, we scale even further to
    # determine our ROI.
    scaled_bboxes = drawing_utils.scale_bboxes(bboxes, 1.2)

    h, w, _ = frame.shape

    # Very much hard coded for our particular use case.
    for i, bbox in enumerate(scaled_bboxes):
        if bbox is None: continue

        # Grab the part that we care about.
        rounded_bbox = bbox.astype(int)
        top_left = rounded_bbox[:2]
        bottom_right = top_left + rounded_bbox[2:]
        xs = np.clip([top_left[0], bottom_right[0]], 0, w)
        ys = np.clip([top_left[1], bottom_right[1]], 0, h)

        roi = frame[ys[0]:ys[1], xs[0]:xs[1]]

        # Resize the roi to be a reasonable dimension to see
        # Make the smaller of the two dimensions a fixed size
        IMAGE_SIZE = 100
        roi_h, roi_w, _ = roi.shape
        sf = IMAGE_SIZE / min(roi_h, roi_w)
        roi = cv2.resize(roi, (0, 0), fx=sf, fy=sf)

        new_bbox = None
        cls = classes[i]
        if cls == 'w':
            # TODO: Tune parameters here, if necessary
            print("Refining white whiffle ball")
            gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)

            min_radius = IMAGE_SIZE // 4
            circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, dp=1,
                    minDist=IMAGE_SIZE/2, param1=30, param2=50,
                    minRadius=min_radius,
                    maxRadius=IMAGE_SIZE//2)
            if circles is None:
                print("NO CIRCLES DETECTED. UHHHH")
                continue

            # Find the biggest circle by area, aka biggest radius
            biggest_circle_index = np.argmax(circles[0, :, 2])
            biggest_circle = circles[0, biggest_circle_index]
            c = biggest_circle

            if (c[2] < min_radius):
                print("Got an invalid circle?")
                continue

            # draw the outer circle and a dot at the center
            cv2.circle(roi, (c[0], c[1]), c[2], (0, 255, 0), 2)
            cv2.circle(roi, (c[0], c[1]), 2, (0, 0, 255), 3)

            # Use the bounding box of the circle to reinitialize the tracker.
            new_bbox = np.array([c[0] - c[2], c[1] - c[2], 2 * c[2], 2 * c[2]])


        elif cls == 'c':
            print("Refining orange cube")
            hsv = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
            hsv_blurred = cv2.GaussianBlur(hsv, (5, 5), 0)
            ret, thresh_h = cv2.threshold(hsv_blurred[:, :, 0], 30, 255,
                    cv2.THRESH_BINARY_INV)
            ret, thresh_s = cv2.threshold(hsv_blurred[:, :, 1], 0, 255,
                    cv2.THRESH_BINARY | cv2.THRESH_OTSU)
            mask = cv2.bitwise_and(thresh_h, thresh_s)


            # Clean up the mask a little
            kernel = np.ones((11,11),np.uint8)
            mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
            #  cv2.imshow("Opening", opening)


            roi = cv2.bitwise_and(roi, roi, mask=mask)
            print("made the roi from the mask")

            # Grab the bounding box from the mask
            conn_stats = cv2.connectedComponentsWithStats(mask, connectivity=4)
            retval, labels, stats, centroids = conn_stats

            # The stats tell us [top left, top right, width, height, area]
            # Find the label with the biggest area
            if len(stats) > 1: # Means we have a non-bg label
                biggest_label = np.argmax(stats[1:, -1]) + 1

                p1 = stats[biggest_label, :2]
                p2 = p1 + stats[biggest_label, 2:-1]
                cv2.rectangle(roi, tuple(p1.astype(int)), tuple(p2.astype(int)), color=(255, 0, 100))
                print("drew the rectangle")

                new_bbox = stats[biggest_label, :-1]

        cv2.imshow("Image %d" % i, roi)

        if new_bbox is None:
            continue

        print("New bounding box", new_bbox)
        new_bbox = new_bbox / sf # Unscale by the same amount we scaled
        new_bbox = np.array([*(top_left + new_bbox[:2]), *new_bbox[2:]])

        print("Replacing bbox %d" % i, rounded_bbox, new_bbox)

        # Scale the bbox by the proper scale factor
        new_bbox_scaled = drawing_utils.scale_bboxes([new_bbox], args.scale)
        new_bbox_scaled = clamp_bboxes(new_bbox_scaled, w, h)

        # Force the scaled bounding box to be inside the bounds of the image.
        #  if any(new_bbox < 0):
        #      input()


        print("Initializing tracker")
        # Apply the new scaled bbox to both the tracker and the saved ones
        new_tracker = init_trackers(args.tracker, frame, new_bbox_scaled)[0]
        trackers[i] = new_tracker
        bboxes[i] = new_bbox_scaled[0]
        print("new scaled bbox", bboxes[i])
示例#46
0
    #frame = cv.imread('images/smarties.png')
    # if resl == False:
    #     print('There is no Video')
    #     break
    
    #resize the resolution of the frame
    ratio = 0.6
    new_width = int(frame.shape[1] * ratio)
    new_height = int(frame.shape[0] * ratio)
    #dim = (new_width, new_height)
    dim = (640,480)
    resized_frame = cv.resize(frame,dim, interpolation = cv.INTER_AREA)
    #add Blur to frame to reduce noise
    resized_frame = cv.GaussianBlur(resized_frame, (5,5), 0)
    #convert BGR image into  HSV
    hsv = cv.cvtColor(resized_frame,cv.COLOR_BGR2HSV)

    #* get the value from Trackbar
    # Get color in a range (hueLower,hueUpper) to get wide cover than a number
    hueLower = cv.getTrackbarPos('hueLower','Trackbars')
    hueUpper = cv.getTrackbarPos('hueUpper', 'Trackbars')
    # Pick another hue section to show 
    hL2 = cv.getTrackbarPos('hL2','Trackbars')
    hU2 = cv.getTrackbarPos('hU2', 'Trackbars')

    # Saturation from a range not by specific number to get wideq cover
    satLow = cv.getTrackbarPos('satLow','Trackbars')
    satHigh = cv.getTrackbarPos('satHigh', 'Trackbars')

    # Lightness
    liLow = cv.getTrackbarPos('valLow','Trackbars')
示例#47
0
def main():
    """
    Read input video and process it, the output video will be exported output_video path
     which can be set by input arguments.
    Example: python inference_video.py --config configs/config.json --input_video_path data/video/sample.mov
     --output_video data/videos/output.avi
    """
    argparse = ArgumentParser()
    argparse.add_argument('--config', type=str, help='json config file path')
    argparse.add_argument('--input_video_path',
                          type=str,
                          help='the path of input video',
                          default='')
    argparse.add_argument('--output_video',
                          type=str,
                          help='the name of output video file',
                          default='face_mask_output.avi')
    args = argparse.parse_args()

    config_path = args.config
    cfg = Config(path=config_path)

    if args.input_video_path == '':
        input_path = cfg.APP_VIDEO_PATH
    else:
        input_path = args.input_video_path

    print("INFO: Input video is: ", input_path)
    output_path = args.output_video
    file_name_size = len(output_path.split('/')[-1])
    output_dir = output_path[:-file_name_size]

    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    print("INFO: The output video will be exported at: ", output_path)

    detector_input_size = (cfg.DETECTOR_INPUT_SIZE[0],
                           cfg.DETECTOR_INPUT_SIZE[1], 3)
    classifier_img_size = (cfg.CLASSIFIER_INPUT_SIZE,
                           cfg.CLASSIFIER_INPUT_SIZE, 3)

    device = cfg.DEVICE
    detector = None
    classifier = None
    output_vidwriter = None

    if device == "x86":
        from libs.detectors.x86.detector import Detector
        from libs.classifiers.x86.classifier import Classifier

    elif device == "EdgeTPU":
        from libs.detectors.edgetpu.detector import Detector
        from libs.classifiers.edgetpu.classifier import Classifier
    elif device == "Jetson":
        from libs.detectors.jetson.detector import Detector
        from libs.classifiers.jetson.classifier import Classifier
    else:
        raise ValueError('Not supported device named: ', device)

    detector = Detector(cfg)
    classifier_model = Classifier(cfg)
    input_cap = cv.VideoCapture(input_path)

    print("INFO: Start inferencing")
    frame_id = 0
    while (input_cap.isOpened()):
        ret, raw_img = input_cap.read()
        if output_vidwriter is None:
            output_vidwriter = cv.VideoWriter(
                output_path, cv.VideoWriter_fourcc('M', 'J', 'P', 'G'), 24,
                (raw_img.shape[1], raw_img.shape[0]))
            height, width = raw_img.shape[:2]

        if ret == False:
            break
        _, cv_image = input_cap.read()
        if np.shape(cv_image) != ():
            resized_image = cv.resize(cv_image, tuple(detector_input_size[:2]))
            rgb_resized_image = cv.cvtColor(resized_image, cv.COLOR_BGR2RGB)
            objects_list = detector.inference(rgb_resized_image)
            faces = []
            cordinates = []
            cordinates_head = []
            for obj in objects_list:
                if 'bbox' in obj.keys():
                    face_bbox = obj['bbox']  # [ymin, xmin, ymax, xmax]
                    xmin, xmax = np.multiply([face_bbox[1], face_bbox[3]],
                                             width)
                    ymin, ymax = np.multiply([face_bbox[0], face_bbox[2]],
                                             height)
                    croped_face = cv_image[int(ymin):int(ymin) +
                                           (int(ymax) - int(ymin)),
                                           int(xmin):int(xmin) +
                                           (int(xmax) - int(xmin))]
                    # Resizing input image
                    croped_face = cv.resize(croped_face,
                                            tuple(classifier_img_size[:2]))
                    croped_face = cv.cvtColor(croped_face, cv.COLOR_BGR2RGB)
                    # Normalizing input image to [0.0-1.0]
                    croped_face = croped_face / 255.0
                    faces.append(croped_face)
                    cordinates.append(
                        [int(xmin), int(ymin),
                         int(xmax), int(ymax)])
                if 'bbox_head' in obj.keys():
                    head_bbox = obj['bbox_head']  # [ymin, xmin, ymax, xmax]
                    xmin, xmax = np.multiply([head_bbox[1], head_bbox[3]],
                                             width)
                    ymin, ymax = np.multiply([head_bbox[0], head_bbox[2]],
                                             height)
                    cordinates_head.append(
                        [int(xmin), int(ymin),
                         int(xmax), int(ymax)])

            faces = np.array(faces)
            face_mask_results, scores = classifier_model.inference(faces)
            for i, cor in enumerate(cordinates):
                if face_mask_results[i] == 1:
                    color = (0, 0, 255)
                elif face_mask_results[i] == 0:
                    color = (0, 255, 0)
                else:
                    color = (0, 0, 0)

                cv.rectangle(raw_img, (cor[0], cor[1]), (cor[2], cor[3]),
                             color, 2)

            for cor in cordinates_head:
                cv.rectangle(raw_img, (cor[0], cor[1]), (cor[2], cor[3]),
                             (200, 200, 200), 2)
            output_vidwriter.write(raw_img)
            print('{} Frames number are processed. {} fps'.format(
                frame_id, detector.fps))
            frame_id = frame_id + 1
        else:
            continue

    input_cap.release()
    output_vidwriter.release()
    print('INFO: Finish:) Output video is exported at: ', output_path)
示例#48
0
    # params for ShiTomasi corner detection
    feature_params = dict( maxCorners = 100,
                        qualityLevel = 0.3,
                        minDistance = 7,
                        blockSize = 7 )

    # Parameters for lucas kanade optical flow
    lk_params = dict( winSize  = (15, 15),
                    maxLevel = 2,
                    criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03))

    # Take first frame
    cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
    ret, old_frame = cap.read()
    old_gray = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY)

    # preserve aspect ratio
    HORIZONTAL_BORDER = 50
    VERTICAL_BORDER = (HORIZONTAL_BORDER*old_gray.shape[1])/old_gray.shape[0]

    print '--Generation--'
    frame_num = 0
    prev_motion = []
    while frame_num < frame_count-2:
        try:
            # processing frames
            ret, frame = cap.read()
            frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

            # find corners in it
示例#49
0
       image = cv2.bitwise_not(~image)


       images.append(image)
       #Read the images from your directory
       


#stitcher = cv2.createStitcher()
stitcher = cv2.Stitcher.create()
ret,pano = stitcher.stitch(images)

if ret==cv2.STITCHER_OK:
    
    #need to swap colors here, demosaicing algoithm in rawpy jumbles to colors for some reason?:
    truepano = cv2.cvtColor(pano,cv2.COLOR_BGR2RGB)
    
    #Apply gamma corrections: gamma values greater than 1 will shift the image histogram towards left and the output image will be darker than the input image. On the other hand, for gamma values less than 1, the histogram will shift towards right and the output image will be brighter than the input image.
    

    gamma_corrected_pano = exposure.adjust_gamma(truepano, gamma=1, gain=0.5)

       
    panoimage=gamma_corrected_pano
       
    #apply histogram equalization
    #using skimage (easy way)
    hist_equalized_pano = exposure.equalize_hist(panoimage)
    
    
    panoramaoutfile = "panoresult.png"
示例#50
0
import cv2

cap = cv2.VideoCapture(0)

# Add ops to save and restore all the variables.
saver = tf.train.Saver()

with tf.Session() as sess:
    # Restore variables from disk.
    saver.restore(sess, "./tmp/model.ckpt")
    print("Model restored.")

    while True:
        ret, img = cap.read()  # 720x1280x3 <-- print(img.shape);

        resized = cv2.resize(img, (140, 80), interpolation=cv2.INTER_AREA)
        #cropped = resized[0:180, 70:250]
        #resized64 = cv2.resize(cropped, (128, 128), interpolation=cv2.INTER_AREA)
        gray = np.asarray(cv2.cvtColor(resized, 7))

        cv2.imshow('Capture', gray)
        frame = gray.reshape(-1, 80, 140, 1)
        print(sess.run(y, feed_dict={x: frame}))

        ch = 0xFF & cv2.waitKey(1)
        if ch == 27:
            break

cv2.destroyAllWindows()
from __future__ import print_function
import argparse
import numpy as np
import mahotas
import cv2
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True, help="Path to the image")
args = vars(ap.parse_args())

image = cv2.imread(args["image"])
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(image, (5, 5), 0)
cv2.imshow("Image", image)

T = mahotas.thresholding.otsu(blurred)
print("Otsu's threshold {}".format(T))

thresh = image.copy()
thresh[thresh > T] = 255
thresh[thresh < 255] = 0
thresh = cv2.bitwise_not(thresh)
cv2.imshow("Otsu", thresh)

T = mahotas.thresholding.rc(blurred)
print("Riddler-calvard:{}".format(T))
thresh = image.copy()
thresh[thresh > T] = 255
thresh[thresh < 255] = 0
thresh = cv2.bitwise_not(thresh)
cv2.imshow("Riddler-calvard", thresh)
cv2.waitKey(0)
import cv2
import numpy as np


path = "./count/DJI_0010.JPG"
ori = cv2.imread(path)
# print(ori.shape)  # (3078, 5472, 3)

re_ori = cv2.resize(ori, (1368, 770), interpolation=cv2.INTER_AREA)

# HSV
hsv = cv2.cvtColor(re_ori, cv2.COLOR_BGR2HSV)
hsv_f = cv2.cvtColor(re_ori, cv2.COLOR_BGR2HSV_FULL)

cv2.imshow("hsv", hsv)
cv2.imshow("hsv_f", hsv_f)

cv2.waitKey(0)
cv2.destroyAllWindows()
示例#53
0
def run_recognize(cameraId, scaleFactor, minSizeTuple, tolerance, minNeighbour,
                  apiService, runMode, showDetailInfo):
    try:
        global whoIsLocked, inActionLock
        timeOfLock = None
        currentPerson = None
        alpha = 1.20  # Contrast control (1.0-3.0)
        beta = 21  # Brightness control (0-100)

        # Buttons to GPIO pins (physical numbering)
        buttonStart = 11
        buttonBreak = 21
        buttonTask = 22
        buttonEnd = 24
        buzzerPin = 13

        bounceTime = 230  # Used when setting up events as bounce prevent time
        buzzerDutyCycle = 0.7
        lockInTime = 6.5  # How much time user has to choose action

        display = lcddriver.lcd(
        )  # My display has 16 characters maximum; 2 lines

        GPIO.setmode(GPIO.BOARD)  # Use physical pin numbering
        GPIO.setup(buttonStart, GPIO.IN, pull_up_down=GPIO.PUD_UP)
        GPIO.setup(buttonEnd, GPIO.IN, pull_up_down=GPIO.PUD_UP)
        GPIO.setup(buttonBreak, GPIO.IN, pull_up_down=GPIO.PUD_UP)
        GPIO.setup(buttonTask, GPIO.IN, pull_up_down=GPIO.PUD_UP)
        GPIO.setup(buzzerPin, GPIO.OUT)
        buzzer = GPIO.PWM(buzzerPin, 1200)

        def map_button_to_eventId(button):
            if button == buttonStart:
                return 1
            elif button == buttonBreak:
                return 2
            elif button == buttonTask:
                return 3
            elif button == buttonEnd:
                return 4
            else:
                raise Exception(
                    f'Button not mapped to any event. GPIO pin: {button}')

        def event_callback(button):
            # Setting up globals
            global whoIsLocked, inActionLock, lastPersonEntry

            if inActionLock:
                buzzer_quick_alert(buzzer, buzzerDutyCycle)
                print(
                    f'[INFO] [{strftime("%m-%d %H:%M:%S", getLocalTime())}] Action prevented, ongoing action.'
                )
                return
            if whoIsLocked is None:
                buzzer_quick_alert(buzzer, buzzerDutyCycle)
                print(
                    f'[INFO] [{strftime("%m-%d %H:%M:%S", getLocalTime())}] Action prevented, nobody in lock.'
                )
                return

            actionTime = getCurrentTime()

            # To prevent button bouncing
            #if (lastPersonEntry is not None and
            #    lastPersonEntry.time+9 > actionTime):
            #    # remove print in final version
            #    if showDetailInfo:
            #        print('[INFO] Bounce prevented (not enough time passed between actions.')
            #    return

            eventId = map_button_to_eventId(button)

            # Prevent bouncing if last person = current person in X seconds timeframe
            if (lastPersonEntry is not None
                    and lastPersonEntry.personId == whoIsLocked[0]
                    and lastPersonEntry.eventId == eventId
                    and actionTime < lastPersonEntry.time + 20):
                if showDetailInfo:
                    print(
                        f'[INFO] [{strftime("%m-%d %H:%M:%S", getLocalTime())}] Action prevented. Same person, same action. Minimum time has not passed. Time remaining is {(round(lastPersonEntry.time+20 - actionTime, 1))}s.'
                    )
                return

            inActionLock = True  # Running the command, no interrupts
            display.lcd_clear()

            # This is new last person
            lastPersonEntry = LastPersonEntry(actionTime, eventId,
                                              whoIsLocked[0])

            if whoIsLocked[0] is None:  # id is None which means user is Unknown
                print(
                    f'[{strftime("%m-%d %H:%M:%S", getLocalTime())}] Message -> Person not recognized, please look at camera and try again.'
                )
                response = apiService.post_action(None, eventId)
                if response.serverError:
                    print(
                        f'[{strftime("%m-%d %H:%M:%S", getLocalTime())}] [ERROR] Server error.'
                    )
                display.lcd_display_string("Not recognized", 1)
                display.lcd_display_string("Please try again", 2)
                buzzer_error(buzzer, buzzerDutyCycle)
            else:  # User is known
                response = apiService.post_action(whoIsLocked[0], eventId)
                if showDetailInfo:
                    print(
                        f'[INFO] [{strftime("%m-%d %H:%M:%S", getLocalTime())}] User  id is -> {whoIsLocked[0]}'
                    )
                if not response.serverError:
                    if response.message is not None:
                        print(
                            f'[{strftime("%m-%d %H:%M:%S", getLocalTime())}] Message -> {response.message}'
                        )
                        if response.messageCode == 1:
                            display.lcd_display_string("  Work already", 1)
                            display.lcd_display_string("    started", 2)
                            buzzer_error(buzzer, buzzerDutyCycle)
                        elif response.messageCode == 2:
                            display.lcd_display_string("    Work not", 1)
                            display.lcd_display_string("    started", 2)
                            buzzer_error(buzzer, buzzerDutyCycle)
                        elif response.messageCode == 3:
                            display.lcd_display_string("   Break not", 1)
                            display.lcd_display_string("     closed", 2)
                            buzzer_error(buzzer, buzzerDutyCycle)
                        elif response.messageCode == 4:
                            display.lcd_display_string("Welcome", 1)
                            display.lcd_display_string(whoIsLocked[1], 2)
                            buzzer_ok(buzzer, buzzerDutyCycle)
                        elif response.messageCode == 5:
                            display.lcd_display_string("Have fun", 1)
                            display.lcd_display_string(whoIsLocked[1], 2)
                            buzzer_ok(buzzer, buzzerDutyCycle)
                        elif response.messageCode == 6:
                            display.lcd_display_string("Stay safe", 1)
                            display.lcd_display_string(whoIsLocked[1], 2)
                            buzzer_ok(buzzer, buzzerDutyCycle)
                        elif response.messageCode == 7:
                            display.lcd_display_string("Goodbye", 1)
                            display.lcd_display_string(whoIsLocked[1], 2)
                            buzzer_ok(buzzer, buzzerDutyCycle)
                        elif response.messageCode == 8:
                            display.lcd_display_string("Welcome back", 1)
                            display.lcd_display_string(whoIsLocked[1], 2)
                            buzzer_ok(buzzer, buzzerDutyCycle)
                        elif response.messageCode == 9:
                            display.lcd_display_string("  Official AB.", 1)
                            display.lcd_display_string("   not closed", 2)
                            buzzer_error(buzzer, buzzerDutyCycle)
                        elif response.messageCode == 10:
                            display.lcd_display_string("Not recognized", 1)
                            display.lcd_display_string("Please try again", 2)
                            buzzer_error(buzzer, buzzerDutyCycle)
                            if showDetailInfo:
                                print('[WARNING] Message code 9 appeared.')
                        else:
                            display.lcd_display_string("Unknown message", 1)
                            display.lcd_display_string("      code", 2)
                else:
                    display.lcd_display_string("  Server error", 1)
                    buzzer_error(buzzer, buzzerDutyCycle)
            sleep(3.9)  # Shows lcd text and locks actions for time
            display.lcd_clear()
            inActionLock = False

        GPIO.add_event_detect(buttonStart,
                              GPIO.RISING,
                              callback=lambda x: event_callback(buttonStart),
                              bouncetime=bounceTime)
        GPIO.add_event_detect(buttonEnd,
                              GPIO.RISING,
                              callback=lambda x: event_callback(buttonEnd),
                              bouncetime=bounceTime)
        GPIO.add_event_detect(buttonBreak,
                              GPIO.RISING,
                              callback=lambda x: event_callback(buttonBreak),
                              bouncetime=bounceTime)
        GPIO.add_event_detect(buttonTask,
                              GPIO.RISING,
                              callback=lambda x: event_callback(buttonTask),
                              bouncetime=bounceTime)

        print(
            f'[INFO] [{strftime("%m-%d %H:%M:%S", getLocalTime())}] Loading encodings from file.'
        )
        try:
            # Using absolute path, take caution
            data = pickle.loads(
                open(
                    '/home/pi/Desktop/face_recognition_for_attendance_rpi/encodings.pickle',
                    'rb').read())
        except Exception as e:
            print(f'[ERROR] No faces in the model. Error: {e}')
            raise Exception('Error on loading pickle file.')

        detector = cv2.CascadeClassifier(cv2.data.haarcascades +
                                         'haarcascade_frontalface_default.xml')
        print(
            f'[INFO] [{strftime("%m-%d %H:%M:%S", getLocalTime())}] Starting video stream, press "q" to exit.'
        )
        vs = VideoStream(src=cameraId).start()
        sleep(1.3)  # Warm up

        while True:
            thisFrameTime = getCurrentTime()

            frame = vs.read()
            # choose lower width for performance
            frame = resize(frame, width=700)
            # increase brightness and contrast for a bit
            frame = cv2.convertScaleAbs(frame, alpha=alpha, beta=beta)

            # 1) BGR to grayscale: for face detection
            # 2) BGR to RGB: for face recognition
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

            # grayscale frame
            # detectMultiScale autoconverts to greyscale if not in greyscale
            rects = detector.detectMultiScale(gray,
                                              scaleFactor=scaleFactor,
                                              minNeighbors=minNeighbour,
                                              minSize=minSizeTuple,
                                              flags=cv2.CASCADE_SCALE_IMAGE)

            # prepare coordinates for face_recognition function
            boxes = [(y, x + w, y + h, x) for (x, y, w, h) in rects]

            # get biggest box
            biggestBoxInList = getBiggestBoxInList(boxes)

            encodings = face_recognition.face_encodings(rgb, biggestBoxInList)
            names = []
            for encoding in encodings:
                matches = face_recognition.compare_faces(data['encodings'],
                                                         encoding,
                                                         tolerance=tolerance)
                name = (None, 'Unknown')
                if True in matches:
                    matchedIds = [i for (i, b) in enumerate(matches) if b]
                    counts = {}
                    for i in matchedIds:
                        name = data['names'][i]
                        counts[name] = counts.get(name, 0) + 1
                    name = max(counts, key=counts.get)

                    # split id and name
                    splitName = name.split(' ', 1)
                    # set name to tuple (id, user)
                    name = (splitName[0], splitName[1])
                names.append(name)

            if len(names) > 0:
                currentPerson = names[0]  # Pick first and only from array

                if whoIsLocked is None and inActionLock == False:
                    # perpare name because display has 16 chars max
                    if (currentPerson[0] is not None
                            and len(currentPerson[1]) > 16):
                        currentPerson = (currentPerson[0],
                                         currentPerson[1][0:16])

                    display.lcd_clear()
                    display.lcd_display_string("Choose input", 1)
                    display.lcd_display_string(currentPerson[1], 2)
                    timeOfLock = thisFrameTime
                    # Setting variable to tuple (id/None,user/Unknown)
                    whoIsLocked = currentPerson
            else:
                currentPerson = None

            if whoIsLocked is not None and inActionLock == False:
                # first check: if initial lock-on was on Unknown but now we have real user, if that happens lock in on real user
                # second check is to give user enough time to choose input
                if (whoIsLocked[0] is None and currentPerson is not None
                        and currentPerson[0] is not None):
                    whoIsLocked = None
                    display.lcd_clear()
                    timeOfLock = thisFrameTime  # refresh time of lock
                    display.lcd_display_string("Choose input", 1)
                    display.lcd_display_string(currentPerson[1], 2)
                    sleep(0.1
                          )  # delay a bit to let display fully load characters
                    whoIsLocked = currentPerson
                elif timeOfLock + lockInTime < thisFrameTime:
                    whoIsLocked = None
                    display.lcd_clear()

            # This is used just to show who is locked in on video feedback
            if runMode == 1 and whoIsLocked is not None:
                timeLeft = round(timeOfLock + lockInTime - thisFrameTime, 1)
                if timeLeft > 0:  # Countdown goes on if action ran
                    # WhoIsLocked[1] is name/Unknown
                    cv2.putText(frame, f'{whoIsLocked[1]} ({timeLeft}s)',
                                (38, 38), cv2.FONT_HERSHEY_SIMPLEX, 1.5,
                                (0, 0, 255), 3)

            # This is for drawing on screen and can be disabled if no display
            if runMode == 1:
                for ((top, right, bottom, left),
                     name) in zip(biggestBoxInList, names):
                    cv2.rectangle(frame, (left, top), (right, bottom),
                                  (0, 255, 0), 2)
                    y = top - 15 if top - 15 > 15 else top + 15
                    cv2.putText(frame, currentPerson[1], (left, y),
                                cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
                # display video
                cv2.imshow('Camera', frame)

            key = cv2.waitKey(1) & 0xFF
            if key == ord("q"):
                break

    finally:
        # cleanup
        cv2.destroyAllWindows()
        vs.stop()
        buzzer.stop()
        display.lcd_clear()
        GPIO.cleanup()
        print(
            f'[INFO] [{strftime("%m-%d %H:%M:%S", getLocalTime())}] Recognizer finished.'
        )
示例#54
0
        if RANDOM_LIGHTING:
            rgb_dir = os.path.join(scene_dir, "random_lighting_cam0", "data")
        else:
            rgb_dir = os.path.join(scene_dir, "cam0", "data")

        print("{}% completed; computing scene {}".format(
            i * 1.0 / len(files_list[:100]) * 100., scene_dir))

        frame_list = [
            os.path.join(rgb_dir, name) for name in os.listdir(rgb_dir)
            if os.path.isfile(os.path.join(rgb_dir, name))
        ]

        for frame_path in frame_list:
            frame = cv2.imread(frame_path)
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            frame = frame.astype('float32') / 255.

            pts, desc, heatmap = fe.run(frame)

            if desc is not None:
                assert (desc.shape[0] == 256)
                descriptors.append(np.transpose(desc))

    all_embeddings = np.vstack(descriptors)
    print("Starting k means clustering.")
    kmeans = sc.MiniBatchKMeans(n_clusters=VOCABULARY_DIM)
    kmeans.fit(all_embeddings)
    print("Finshed clustering")
    with open(vocabulary_path, 'wb') as f:
        pickle.dump(kmeans, f)
示例#55
0
def grayscale_image(im):
    return cv2.cvtColor(im, cv2.COLOR_RGB2GRAY)
def call_detect():
    global finalImage
    global avgX
    global avgY
    global detected

    h, w = template.shape

    while True:
        #start = time.time()
        clone = image
        gray = cv2.cvtColor(clone, cv2.COLOR_BGR2GRAY)
        found = None
        # loop over the scales of the image
        for scale in np.linspace(2.4, 0.2, 10)[::-1]:
            # resize the image according to the scale, and keep track
            # of the ratio of the resizing
            resized = imutils.resize(gray, width=int(gray.shape[1] * scale))
            r = gray.shape[1] / float(resized.shape[1])
            # if the resized image is smaller than the template, then break
            # from the loop
            if resized.shape[0] < tH or resized.shape[1] < tW:
                break
            # detect edges in the resized, grayscale image and apply template
            # matching to find the template in the image

            edged = cv2.Canny(resized, 50, 200)
            result = cv2.matchTemplate(edged, template, cv2.TM_CCOEFF)
            (_, maxVal, _, maxLoc) = cv2.minMaxLoc(result)

            # if we have found a new maximum correlation value, then ipdate
            # the bookkeeping variable
            if found is None or maxVal > found[0]:
                found = (maxVal, maxLoc, r)

                (startX, startY) = (int(maxLoc[0] * r), int(maxLoc[1] * r))
                (endX, endY) = (int((maxLoc[0] + tW) * r), int((maxLoc[1] + tH) * r))

                resized = gray[startY:endY, startX:endX]

                resized = cv2.resize(resized, (w, h))

                if ssim(original, resized) > 0.2:
                    break

        # unpack the bookkeeping varaible and compute the (x, y) coordinates
        # of the bounding box based on the resized ratio

        (_, maxLoc, r) = found
        (startX, startY) = (int(maxLoc[0] * r), int(maxLoc[1] * r))
        (endX, endY) = (int((maxLoc[0] + tW) * r), int((maxLoc[1] + tH) * r))

        resized = gray[startY:endY, startX:endX]

        resized = cv2.resize(resized, (w, h))
        if ssim(original, resized) > 0.2:
            # draw a bounding box around the detected result and display the image
            cv2.rectangle(clone, (startX, startY), (endX, endY), (0, 0, 255), 2)
            finalImage = clone
            avgX = (startX + endX) / 2
            avgY = (startY + endY) / 2
            detected = True
        else:
            detected = False
示例#57
0
文件: img.py 项目: sthysel/rakali
 def grey(self):
     """Grey image"""
     self.mat = cv.cvtColor(self.mat, cv.COLOR_BGR2GRAY)
     return self
示例#58
0
def detect():
    global corners
    change = False
    firstFrame = None
    finalFrame = None
    for i in range(50):
        (grabbed, f1) = camera.read()
    initialFrame = imutils.resize(f1, width=500)
    (h1,w1) = initialFrame.shape[:2]
    cv2.imshow('initial before loop',initialFrame)
    counter = 0
    while True:
        counter+=1
        (grabbed, frame) = camera.read()
        text = "Unoccupied"

        if not grabbed:
            break

        frame = imutils.resize(frame, width=500)
        #frame = imutils.resize(frame,width=320,height=320)
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        gray = cv2.GaussianBlur(gray, (21, 21), 0)

        if firstFrame is None:
            firstFrame = gray
            continue
        if(len(corners)==4 and corners[3]!=None):
            M = cv2.getPerspectiveTransform(np.float32(corners), np.float32([(0, 0), (320, 0), (0, 320), (320, 320)]))
            frame = cv2.warpPerspective(frame, M, (320, 320))
            frameDelta = cv2.absdiff(firstFrame, gray)
            thresh = cv2.threshold(frameDelta, 25, 255, cv2.THRESH_BINARY)[1]

            thresh = cv2.dilate(thresh, None, iterations=2)
            (_, cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
                                            cv2.CHAIN_APPROX_SIMPLE)

            for c in cnts:
                if cv2.contourArea(c) < min_area:
                    continue
                (x, y, w, h) = cv2.boundingRect(c)
                (x,y,w,h) = (x-corners[0][0],y-corners[1][1],w,h)
                cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
                text = "Occupied"
                change = True

        '''cv2.putText(frame, "Room Status: {}".format(text), (10, 20),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
            cv2.putText(frame, datetime.datetime.now().strftime("%A %d %B %Y %I:%M:%S%p"),
                    (10, frame.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 0, 255), 1)'''

        cv2.imshow("Security Feed", frame)
        cv2.setMouseCallback('Security Feed',mousePos)
        if cnt >4:
            cv2.setMouseCallback('Security Feed',None)
        #cv2.imshow("Thresh", thresh)
        #cv2.imshow("Frame Delta", frameDelta)
        key = cv2.waitKey(1) & 0xFF
        '''if acc==False and counter%50==0:
            temp,corners = a.chess_corners_HSV(initialFrame)
            M = cv2.getPerspectiveTransform(np.float32(corners), np.float32([(0, 0), (320, 0), (0, 320), (320, 320)]))
            temp = cv2.warpPerspective(initialFrame,M, (320, 320))
            cv2.imshow('initFrame',temp)
            time.sleep(3)
            ans = input("Is this the right transformation?")
            if ans=='y' or ans=='Y':
                acc = True
                '''
        if text == "Unoccupied" and change == True:
            time.sleep(3)
            (ret, finalFrame) = camera.read()
            break

        if key == ord("q"):
            break

    '''rows = finalFrame.shape[0]
    cols = finalFrame.shape[1]
    M = cv2.getRotationMatrix2D((cols / 2, rows / 2), -90, 1)
    finalFrame = cv2.warpAffine(finalFrame, M, (cols, rows))
    rows = initialFrame.shape[0]
    cols = initialFrame.shape[1]
    M = cv2.getRotationMatrix2D((cols / 2, rows / 2), -90, 1)
    initialFrame = cv2.warpAffine(initialFrame, M, (cols, rows))
    cv2.imshow('img', initialFrame)
    #cv2.imshow('im', finalFrame)
    print(a.determine_move(initialFrame,finalFrame))
    # finalFrame = adjust_gamma(finalFrame,5)
    finalFrame = a.chess_corners_HSV(finalFrame)
    #initialFrame = a.chess_corners(initialFrame)
    cv2.imshow('final', finalFrame)
    cv2.imshow('img', initialFrame)'''
    
    print('Checkpoint 1')
    cv2.imshow('initial after loop',initialFrame)
    finalFrame = imutils.resize(finalFrame, width=500)
    #initialFrame,corners = a.chess_corners_HSV(initialFrame)
    #finalFrame,corners1 = a.chess_corners_HSV(finalFrame,corners)
    M = cv2.getPerspectiveTransform(np.float32(corners), np.float32([(0, 0), (320, 0), (0, 320), (320, 320)]))
    initialFrame = cv2.warpPerspective(initialFrame, M, (320, 320))
    finalFrame = cv2.warpPerspective(finalFrame, M, (320, 320))
    '''for i in range(4):
        cv2.circle(finalFrame,(corners[i][0],corners[i][1]), 3, (0,0,255), -1)'''
    #finalFrame = a.chess_corners_HSV(finalFrame,corners)[0]
    rows = finalFrame.shape[0]
    cols = finalFrame.shape[1]
    M = cv2.getRotationMatrix2D((cols / 2, rows / 2), -90, 1)
    finalFrame = cv2.warpAffine(finalFrame, M, (cols, rows))
    rows = initialFrame.shape[0]
    cols = initialFrame.shape[1]
    M = cv2.getRotationMatrix2D((cols / 2, rows / 2), -90, 1)
    initialFrame = cv2.warpAffine(initialFrame, M, (cols, rows))
    #cv2.imshow('final',finalFrame)
    '''initialFrame = cv2.cvtColor(initialFrame, cv2.COLOR_BGR2GRAY)
    finalFrame = cv2.cvtColor(finalFrame,cv2.COLOR_BGR2GRAY)
    frameDelta = cv2.absdiff(initialFrame, finalFrame)
    thresh = cv2.threshold(frameDelta, 25, 255, cv2.THRESH_BINARY)[1]
    thresh = cv2.dilate(thresh, None, iterations=2)'''
    #cv2.imshow('thresh',thresh)
    rows = thresh.shape[0]
    cols = thresh.shape[1]
    M = cv2.getRotationMatrix2D((cols / 2, rows / 2), -90, 1)
    thresh = cv2.warpAffine(thresh, M, (cols, rows))
    cv2.imshow('initial',initialFrame)
    cv2.imshow('final',finalFrame)
    #cv2.imshow('thresh',thresh)
    '''initial = imutils.resize(initialFrame, width=500)
    initial = cv2.cvtColor(initial, cv2.COLOR_BGR2GRAY)
    initial = cv2.GaussianBlur(initial, (21, 21), 0)
    final = imutils.resize(finalFrame, width=500)
    final = cv2.cvtColor(final, cv2.COLOR_BGR2GRAY)
    final = cv2.GaussianBlur(final, (21, 21), 0)
    frameDelta = cv2.absdiff(initial, final)
    thresh = cv2.threshold(frameDelta, 25, 255, cv2.THRESH_BINARY)[1]
    thresh = cv2.dilate(thresh, None, iterations=2)'''
    '''initialFrame = cv2.cvtColor(initialFrame, cv2.COLOR_BGR2GRAY)
    finalFrame = cv2.cvtColor(finalFrame, cv2.COLOR_BGR2GRAY)
    thresh = cv2.absdiff(initialFrame,finalFrame)'''
    clahe = cv2.createCLAHE(clipLimit=5.0, tileGridSize=(8,8))
    initialFrame1 = cv2.cvtColor(initialFrame, cv2.COLOR_BGR2GRAY)
    initialFrame1 = cv2.GaussianBlur(initialFrame1, (21, 21), 0)
    finalFrame1 = cv2.cvtColor(finalFrame, cv2.COLOR_BGR2GRAY)
    finalFrame1 = cv2.GaussianBlur(finalFrame1, (21, 21), 0)
    frameDelta = cv2.absdiff(initialFrame1, finalFrame1)
    initialFrame1 = clahe.apply(initialFrame1)
    finalFrame1 = clahe.apply(finalFrame1)
    thresh = cv2.threshold(frameDelta, 25, 255, cv2.THRESH_BINARY)[1]
    #res = a.determine_move4(initialFrame1,finalFrame1)
    #print(a.determine_move3(thresh))
    return a.determine_move3(thresh)
示例#59
0
def semiHistogram_detecting_lines(m, s):

    img = cv2.imread(str(m) + str(s) + '.jpg')
    img_file = str(m) + str(s) + '.jpg'
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # 1 - we need dpi for slicing image
    imgPIL = Image.open(img_file)
    dpi = (300, 300)  # default is (300 , 300)
    if 'dpi' in imgPIL.info.keys():
        dpi = imgPIL.info['dpi']
    del imgPIL

    # 2 - use erod nad then dilate in order to clear small noises
    gray_env = cv2.bitwise_not(gray)

    kernel_dilate = np.ones((5, 5), np.uint8)
    gray_env_dilate = cv2.dilate(gray_env, kernel_dilate, iterations=2)

    # 3 - by semi-histogram way we want to find wasted areas

    slice = int(dpi[0] / 20)
    # cv2.imwrite('find_wasted_round_area_in_documents_1_inv.jpg', gray_env_dilate)

    poly = np.zeros((int(gray_env_dilate.shape[0] / slice),
                     int(gray_env_dilate.shape[1] / slice), 1), np.uint8)
    poly.fill(0)
    pices = (int(gray_env_dilate.shape[0] / slice),
             int(gray_env_dilate.shape[1] / slice))
    for y in range(pices[0]):
        for x in range(pices[1]):
            poly[y,
                 x] = np.mean(gray_env_dilate[(y * slice):((y + 1) * slice),
                                              (x * slice):((x + 1) * slice)])
    _, poly = cv2.threshold(poly, 10, 255, cv2.THRESH_BINARY)
    # cv2.imwrite('find_wasted_round_area_in_documents_2_poly_1.jpg', poly)

    poly2 = np.zeros((int(gray_env_dilate.shape[0] / slice),
                      int(gray_env_dilate.shape[1] / slice), 1), np.uint8)
    poly2.fill(0)
    for y in range(2, pices[0] - 2):
        for x in range(2, pices[1] - 2):
            if (np.mean(poly[y - 2:y + 3, x - 2:x + 3]) > 10):
                poly2[y - 2:y + 3, x - 2:x + 3] = 255
            else:
                poly2[y, x] = 0

    # cv2.imwrite('find_wasted_round_area_in_documents_4_poly2.jpg', poly2)

    del poly
    poly3 = np.zeros(
        (int(gray_env_dilate.shape[0]), int(gray_env_dilate.shape[1]), 1),
        np.uint8)
    poly3.fill(0)
    for y in range(0, pices[0]):
        for x in range(0, pices[1]):
            poly3[(y * slice):((y + 1) * slice),
                  (x * slice):((x + 1) * slice)] = poly2[y, x]

    # cv2.imwrite('find_wasted_round_area_in_documents_5_poly3.jpg', poly3)
    del poly2

    contours, _ = cv2.findContours(poly3, cv2.RETR_EXTERNAL,
                                   cv2.CHAIN_APPROX_SIMPLE)

    c = 1
    for cnt in contours[:]:
        rect = cv2.minAreaRect(cnt)
        box = np.int0(cv2.boxPoints(rect))

        first_sorted = sorted(box, key=lambda l: l[0])
        lefts = first_sorted[0:2]
        rights = first_sorted[2:]
        tmp = sorted(lefts, key=lambda l: l[1])
        top_left = tmp[0]
        down_left = tmp[1]

        tmp = sorted(rights, key=lambda l: l[1])
        top_right = tmp[0]
        down_right = tmp[1]

        if (((top_left[1] - down_left[1])**2 +
             (top_left[0] - down_left[0])**2) <
            ((top_left[1] - top_right[1])**2 +
             (top_left[0] - top_right[0])**2)):
            # print ('horosontal',c)

            y1 = down_left[0]
            x1 = down_left[1]
            y2 = down_right[0]
            x2 = down_right[1]
            slop = (x2 - x1) / (y1 - y2)
            degree = (np.arctan(slop) / np.pi) * 180
            # print(x1 , y1 , x2 , y2)
            # print('slop: ',(np.arctan(slop)/np.pi)*180)

        else:
            # print ('vertical')
            y1 = down_left[0]
            x1 = down_left[1]
            y2 = top_left[0]
            x2 = top_left[1]
            if y1 != y2:
                slop = (x2 - x1) / (y1 - y2)
            else:
                slop = 90
            degree = (np.arctan(slop) / np.pi) * 180
            # print(x1 , y1 , x2 , y2)
            # print('slop: ', (np.arctan(slop) / np.pi) * 180)

        # img = cv2.drawContours(img,[box],0,(10*c,20*c,30*c),5)

        cv2.putText(img, str(degree), (down_right[0], down_right[1]),
                    cv2.FONT_HERSHEY_SIMPLEX, 1, 0, 2)
        # print (degree)
        if degree > 5:
            x, y, w, h = cv2.boundingRect(cnt)
            # print(c,' must be changed' , ' => ',w,h)
            new_img = img[y:y + h, x:x + w]
            rotated = ndimage.rotate(new_img, -1 * degree)
            cv2.floodFill(rotated, None, (0, 0), (255, 255, 255))
            cv2.imwrite(
                'over_rotated_paragraph_{}_{}_{}.jpg'.format(
                    str(m), str(s), str(c)), rotated)
        c += 1

    cv2.imwrite(str(m) + str(s) + '_semi_histogram.jpg', img)
def hueFinder(image, verbosity=0):
	"""Given an image of a fruit, it finds the center of the fruit and draws a radius from the center to approximate the hue."""
	image_bw = cv2.cvtColor(image.copy(), cv2.COLOR_BGR2GRAY)
	th1 = cv2.adaptiveThreshold(image_bw.copy(), 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2)
	image_copy = image.copy()

	radMin = 999
	dpMin =0
	for dp in range(1,10,1):
		if(verbosity>1):
			image = image_copy.copy()
		circ = cv2.HoughCircles(image_bw.copy(), cv2.HOUGH_GRADIENT, dp, minDist = 400, minRadius=80)
		if(circ is not None):
			for c in circ:
				x,y,r =c[0].astype("int")
				if(radMin>r and r<200):
					radMin=r
					dpMin=dp
				if(verbosity>1):
					print(dp)
					cv2.circle(image,(x,y),r,(0,255,0),2)
					showImage(image,title=str(dp),waitTime=500)
		else:
			if(verbosity>1):
				print("Helllo",dp)
	if(verbosity>1):
		image = image_copy.copy()

	circ = cv2.HoughCircles(image_bw.copy(), cv2.HOUGH_GRADIENT, dpMin, minDist = 400, minRadius=80)

	if(circ is not None):
		imageHSV = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
		x,y,r = circ[0,0].astype("int")
		print(radMin)
		if(radMin>110):
			radMin=70
		hues = []
		values = []
		imageMasked = np.zeros(imageHSV.shape[:2])
		for i in range(0,imageHSV.shape[0]):
			for j in range(0,imageHSV.shape[1]):
				dx = i-y
				dy = j-x
				if (((dx**2)+(dy**2)) <= (radMin-10)**2) and imageHSV[i][j][0]<60 and imageHSV[i][j][0]>23:
					imageMasked[i][j]=imageHSV[i][j][0]
					#if(imageHSV[i][j][2]<200):
					hues.append(imageHSV[i][j][0])
					values.append(imageHSV[i][j][2])

		if(verbosity>0):
			# showImage(imageMasked, title="Masked Image", waitTime = 5000)
			plt.imshow(imageMasked)
			plt.colorbar()
			plt.show()

		return ("GREEN" if (0.26307*np.mean(values) + (-1.76579)*np.mean(hues))<(-0.00985) else "YELLOW", np.mean(hues), np.mean(values))

	else:
		cv2.destroyAllWindows()
		return ("UNKNOWN",-1,-1)