コード例 #1
0
def classify(imgName):
    global img
    global FP
    global TP
    loadImage(imgName+'.jpg')
    loadMask(imgName+'.png')
    img=cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    imgHeight=img.shape[0]
    imgWidth=img.shape[1]
    blank_image =np.zeros((imgHeight, imgWidth, 1), np.uint8)
    for x in range(0, imgHeight):
        for y in range(0,imgWidth):
            A=img[x,y][0]
            A=translate(A,0,255,0,31)
            B=img[x,y][1]
            B=translate(B,0,255,0,31)
            histopeau=hist_fullHSVF[A,B]
            histononpeau=hist_fullHSVR[A,B]
            print (histopeau*percentPeau)/(histopeau*percentPeau+histononpeau*(100-percentPeau))
            if((histopeau*percentPeau)/(histopeau*percentPeau+histononpeau*(100-percentPeau))>0.001):
                blank_image[x,y]=255
                if(mask[x,y]!=0):
                    TP+=1
                else:
                    FP+=1
            else:
                blank_image[x,y]=0
    cv2.imwrite('output2.jpg',blank_image)
    print TP
    print FP
    sys.exit()
コード例 #2
0
ファイル: readMocData.py プロジェクト: amoliu/ein
def main():

    for fname in glob("left/*/*/ein/sceneModel/model.yml"):
        print fname

        f = open(fname) 

        lines = []
        # ignore the %YAML:1.0, because the python parser doesn't handle 1.0.
        f.readline() 

        for line in f:
            # for some reason the python parser doesn't like this line either.
            if "background_pose" in line:
                continue
            lines.append(line)
        data = "\n".join(lines)
        
        ymlobject = yaml.load(data)
        #print ymlobject
        scene = ymlobject["Scene"]
        observed_map = GaussianMap.fromYaml(scene["observed_map"])
        image = observed_map.toImage()
        cv2.imwrite("observed.png", image)
        cv2.imshow("observed map", image)
        
        
        
        
        print "observed map: ", observed_map.width, "x", observed_map.height
        dimage = readMatFromYaml(scene["discrepancy_magnitude"])
        cv2.imshow("discrepancy magnitude", dimage)

        cv2.waitKey(0)
        cv2.destroyAllWindows()
コード例 #3
0
ファイル: part0.py プロジェクト: kmaragos/comp.photo
def main():
  ''' This function applies your split script to images.

  It will search through the images/part0 subfolder, and apply your splitting 
  function to each one. It will then save the resulting images.
  '''
  imagesfolder0 = os.path.abspath(os.path.join(os.curdir, 'images', 'part0'))
  print '''Searching for images in {} folder
  (will ignore red, green, or blue in the name)'''.format(imagesfolder0)

  exts = ['.bmp', '.pbm', '.pgm', '.ppm', '.sr', '.ras', '.jpeg', '.jpg', 
    '.jpe', '.jp2', '.tiff', '.tif', '.png']

  for dirname, dirnames, filenames in os.walk(imagesfolder0):
    for filename in filenames:
      name, ext = os.path.splitext(filename)
      if ext in exts and 'red' not in name and 'green' not in name and \
        'blue' not in name:
        print "Splitting image {}.".format(filename)

        img = cv2.imread(os.path.join(dirname, filename))
        red, green, blue = split_rgb(img)

        for values, color, channel in zip((red, green, blue), 
            ('red', 'green', 'blue'), (2,1,0)):
          img = np.zeros((values.shape[0], values.shape[1], 3), 
              dtype = values.dtype) 
          img[:,:,channel] = values
          print "Writing image {}.".format(name+color+ext)
          cv2.imwrite(os.path.join(dirname, name+color+ext), img)
コード例 #4
0
def tryconcat():
	flist=[]
	for j in range(8):
		for i in range(16):
			flist.append(cv2.imread('tmp/%d_%d_%d.jpg'%(i,j,1)))
	img=concat_images(flist, 8, 16)
	cv2.imwrite("temp_hd.jpg", img)
コード例 #5
0
def skeletonization(img):
    '''
    http://opencvpython.blogspot.ru/2012/05/skeletonization-using-opencv-python.html
    '''
    img = img.copy()
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    size = np.size(img)
    skel = np.zeros(img.shape, np.uint8)

    # ret, img = cv2.threshold(img, 127, 255, 0)
    img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 7, 2)
    element = cv2.getStructuringElement(cv2.MORPH_CROSS, (3, 3))

    while True:
        eroded = cv2.erode(img, element)
        temp = cv2.dilate(eroded, element)
        temp = cv2.subtract(img, temp)
        skel = cv2.bitwise_or(skel, temp)
        img = eroded.copy()

        zeros = size - cv2.countNonZero(img)
        if zeros == size:
            break

    cv2.imwrite("skel.png", skel)
    return skel
コード例 #6
0
ファイル: do_of.py プロジェクト: electroncastle/DAR
def show_img(data, suffix, show):
    global wnd_x
    global wnd_y

    if data == None:
        return

    image = data.astype(np.uint8)

    if image.shape[0] == 6:
        img = image.transpose(1,2,0)
        img1 = img[:,:,:3]
        img2 = img[:,:,3:]
    else:
        img1 = image[0]
        img2 = image[1]

    filename = 'img-1-'+suffix+'.jpg'
    cv2.imwrite(filename, img1)
    if show:
       cv2.imshow("IMG 1", img1)
       cv2.moveWindow("IMG 1", wnd_x, wnd_y)

    filename = 'img-2-'+suffix+'.jpg'
    cv2.imwrite(filename, img2)
    if show:
       cv2.imshow("IMG 2", img2)
       wnd_x+=300
       cv2.moveWindow("IMG 2",  wnd_x, wnd_y)
       wnd_x=10 
       wnd_y+=300
コード例 #7
0
ファイル: detect.py プロジェクト: furaga/AnimeFaceWinForm
def detect(filename, cascade_file = "lbpcascade_animeface.xml"):
    if not os.path.isfile(cascade_file):
        raise RuntimeError("%s: not found" % cascade_file)

    cascade = cv2.CascadeClassifier(cascade_file)
    image = cv2.imread(filename)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    gray = cv2.equalizeHist(gray)
    
    faces = cascade.detectMultiScale(gray,
                                     # detector options
                                     scaleFactor = 1.1,
                                     minNeighbors = 5,
                                     minSize = (256, 256))
    
    faceCount = 0;
    for (x, y, w, h) in faces:
        print (x, y, w, h)

#        cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 2)

        if len(sys.argv) >= 3:
            filanameWithExtension = os.path.basename(sys.argv[1])
            filename = os.path.splitext(filanameWithExtension)[0]
            savepath = os.path.join(sys.argv[2], filename + "-" + str(faceCount) + ".png");
            cv2.imwrite(savepath, image[y:y+h,x:x+w])

        faceCount = faceCount + 1
コード例 #8
0
ファイル: Resize.py プロジェクト: JRezende/pdi_2013
    def run(self):
        self.ratio = 100
        self.resize_im = array([])
        while True:
            ch = 0xFF & cv2.waitKey(50)
            if ch == 27:
                break

            if ch in [ord('s'), ord('S')]:
                if self.resize_im.any():
                    cv2.imwrite(str(self.filename), self.resize_im)
                    print 'Image Saved.'
                    break

            if ch in [ord('a'), ord('A')]:
                newFilename = QFileDialog.getSaveFileName(self, 'Save File As...', os.getenv('HOME'),
                                                          "Images (*.png *.xpm *.jpg)")
                if newFilename:
                    if self.resize_im.any():
                        cv2.imwrite(str(newFilename), self.resize_im)
                        print 'Image Saved as ' + newFilename
                        break

            if ch in [ord('r'), ord('R')]:
                ratio, ok = QInputDialog.getText(self, 'Set Image Ratio', 'Type ratio value in % (ex: 80): ')
                if ok:
                    if int(ratio) > 0 and int(ratio) <= 100:
                        self.ratio = int(ratio)
                        self.resizeImage()
                    else:
                        print 'Image scale must be between 1 and 100'

        cv2.destroyAllWindows()
コード例 #9
0
ファイル: kitnn.py プロジェクト: obilaniu/IFT6266
def verb_showfilters(argv):
	"""Dump source code of session"""
	
	f  = KITNNFile(argv[2])
	s  = f.getSession(argv[3]).d["snap/1"]
	
	fine   = s["data/2" ][...]
	medium = s["data/8" ][...]
	coarse = s["data/14"][...]
	
	w  = 3+(16*7+15*3)+3
	h  = 3+( 9*7+ 8*3)+3
	
	img= np.zeros((h,w,3), dtype="uint8")
	
	for i in xrange(9):
		for j in xrange(16):
			n     = i*16+j
			if i in [0,1,2]:
				card  = fine  [n- 0]
			elif i in [3,4,5]:
				card  = medium[n-48]
			elif i in [6,7,8]:
				card  = coarse[n-96]
			card -= np.min(card)
			card /= np.max(card)
			card  = card.transpose(1,2,0)
			
			img[3+i*10:3+i*10+7, 3+j*10:3+j*10+7] = 255*card
	
	img = cv2.resize(img, (0,0), None, 8, 8, cv2.INTER_NEAREST)
	cv2.imshow("Filters", img)
	cv2.imwrite("Filters.png", img)
	cv2.waitKey()
コード例 #10
0
def featureMatching(input_keypoint_list, input_xy_list, input_img, template_img):
  #inputとtemplateのkeypoint_binary,xy_listを読み込む
  template_keypoint_list = []
  template_keypoint = csv.reader(open('template_keypoint_binary.csv', 'r'))
  for temp_key in template_keypoint:
    template_keypoint_list.append(map(int,temp_key))
  template_xy_list = []
  template_xy = csv.reader(open('template_keypoint_xy.csv', 'r'))
  for temp_xy in template_xy:
    template_xy_list.append(map(int,temp_xy))
  x_sum_img = len(input_img[0]) + len(template_img[0])
  sum_img = np.zeros((len(input_img), x_sum_img), np.uint8)
  sum_img = np.hstack((input_img, template_img))
  template_add_xsize = []
  #ハミング距離をとって0になったらマッチングとする
  for temp_y in xrange(0,len(template_keypoint_list)):
    for inp_y in xrange(0,len(input_keypoint_list)):
      calc_list = template_keypoint_list[temp_y] - input_keypoint_list[inp_y]
      #print 'template_xy' + str(template_xy_list[temp_y])
      #print 'input_xy' + str(input_xy_list[inp_y])
      if  all((x == 0 for x in calc_list)) == True:
        #マッチングした時のkeypoint_bineryと同じ場所のx,yの値を画像平面上にマッピングし、結果を出力
        print 'get match'
        temp_xy = template_xy_list[temp_y]
        inp_xy = input_xy_list[inp_y]
        template_add_xsize = [temp_xy[1] + len(input_img[0]),temp_xy[0]]
        input_change_xy = [inp_xy[1],inp_xy[0]]
        add_input = tuple(input_change_xy)
        add_template = tuple(template_add_xsize)
        cv2.circle(sum_img,add_input,3,(0,0,0),-1)
        cv2.circle(sum_img,add_template,3,(0,0,255),-1)
        #cv2.line(sum_img,add_input,add_template,(255,255,0),1)
        #print 'add_input = ' +str(add_input) + ' add_template = ' +str(add_template)
  print 'quit calc'
  cv2.imwrite('sum_img.tif', sum_img)
コード例 #11
0
def coolBlack():
    IMAGE_WEIGHT = 0.5

    image = cv2.imread("G:/Filters/wasim.jpg",0)
    black = cv2.imread("G:/Filters/black5.jpg",0)
    black = cv2.resize(black, image.shape[::-1])

    res1 = cv2.addWeighted(image, IMAGE_WEIGHT, black, 1 - IMAGE_WEIGHT, 1)


    #NORMALIZE IMAGES
    image = np.float32(image)
    black = np.float32(black)

    image /= 255
    black /= 200

    res = image*black

    cv2.imshow("RES", res)
    cv2.waitKey(0)

    fname = "G:/Filtes/temp.jpg"
    cv2.imwrite(fname, res)
    res = cv2.imread(fname, 0)

    cv2.imshow("BLACK", res)
    cv2.waitKey(0)
コード例 #12
0
 def exitFrame(self):
     """Draw to the window. Write to files. Release the frame."""
     
     if self.frame is None:
         self._enteredFrame = False
         return
     
     # Update the FPS estimate and related variables.
     if self._framesElapsed == 0:
         self._startTime = time.time()
     else:
         timeElapsed = time.time() - self._startTime
         self._fpsEstimate =  self._framesElapsed / timeElapsed
     self._framesElapsed += 1
     
     # Draw to the window, if any.
     if self.previewWindowManager is not None:
         if self.shouldMirrorPreview:
             mirroredFrame = numpy.fliplr(self._frame).copy()
             self.previewWindowManager.show(mirroredFrame)
         else:
             self.previewWindowManager.show(self._frame)
     
     # Write to the image file, if any.
     if self.isWritingImage:
         cv2.imwrite(self._imageFilename, self._frame)
         self._imageFilename = None
     
     # Write to the video file, if any.
     self._writeVideoFrame()
     
     # Release the frame.
     self._frame = None
     self._enteredFrame = False
コード例 #13
0
ファイル: describe.py プロジェクト: Talamantez/angora-blue
def describeNegativeHelper(imagePath, output):
    outputImagePath = '%s%s' % (imagePath, outputImageExtension)
    image = cv2.imread(imagePath)
    # Save an equalized version of the image.
    cv2.imwrite(outputImagePath, equalizedGray(image))
    # Append the equalized image to the negative description.
    print >> output, outputImagePath
コード例 #14
0
ファイル: calibration.py プロジェクト: alitteneker/CS268
def calibrate(filenames):
    # termination criteria
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
    # prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
    objp = np.zeros((6*7,3), np.float32)
    objp[:,:2] = np.mgrid[0:7,0:6].T.reshape(-1,2)
    # Arrays to store object points and image points from all the images.
    objpoints = [] # 3d point in real world space
    imgpoints = [] # 2d points in image plane.
    images = []
    for filename in filenames:
        # Find the chess board corners. If found, add object points, image points (after refining them)
        img = cv2.imread(filename)
        if img != None:
            print "Loaded " + repr(filename)
        else:
            print "Unable to load image " + repr(filename)
            continue
        images.append(img)
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        ret, corners = cv2.findChessboardCorners(gray, (7,6), None)
        if ret == True:
            objpoints.append(objp)
            corners2 = cv2.cornerSubPix(gray, corners, (11,11), (-1,-1), criteria)
            imgpoints.append(corners2)
    ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1],None,None)
    print "Loaded all images and calbulated calibration"
    for i, img in enumerate(images):
        img = images[i]
        h, w = img.shape[:2]
        newcameramtx, roi = cv2.getOptimalNewCameraMatrix(mtx, dist, (w,h), 1, (w,h))
        dst = cv2.undistort(img, mtx, dist, None, newcameramtx)
        x, y, w, h = roi
        cv2.imwrite( 'calibrated/out_' + str(i) + '.png', dst[ y : y+h, x : x+w ])
        print "Outputted calibrated image: 'calibrated/out_" + str(i) + ".png'"
コード例 #15
0
def Get_wall_coordinates(source, dest):

    x = source;
    y = dest;

# *******************************Image Processing for identifying corners in the image**********************************
    image = cv2.imread(x)

    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    gray = np.float32(gray)
    dst = cv2.cornerHarris(gray, 2, 3, 0.04)
    image[dst > 0.01 * dst.max()] = [0, 0, 255]

    cv2.imwrite('coordinateImage.jpg', image)

# *******************************Getting the pixel values of the detected corners***************************************
    getCoord = np.where(np.all(image == (0, 0, 255), axis=-1))
    coordinates = zip(getCoord[0], getCoord[1])
    x = np.array(coordinates, dtype="int")
    print ("Coordinates : ")
    print (x)

# ******************************* Generating the text file *************************************************************
    filename1 = open(y, "w")
    filename1.write(str(coordinates))
    filename1.close()

    #plt.scatter(getCoord[0], getCoord[1])
    #plt.show()
コード例 #16
0
ファイル: extractDescriptor.py プロジェクト: tiagojc/IBTSFIF
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)
コード例 #17
0
	def run(self):
		runFlag = True
		cv2.namedWindow("TurtleCam 9000", 1)
		while(runFlag):
			image, timesImageServed = self.robot.getImage()
			with self.lock:
				if timesImageServed > 30:
					if self.stalled == False:
						print "Camera Stalled!"
					self.stalled = True
				else:
					self.stalled = False


			frame = self.mcs.update(image.copy())
			cv2.imshow("TurtleCam 9000", frame)

			code = chr(cv2.waitKey(10) & 255)

			if code == 't':
				cv2.imwrite("/home/macalester/catkin_ws/src/speedy_nav/res/captures/cap-" + str(datetime.now()) + ".jpg", image)
				print "Image saved!"
			if code == 'q':
				break

			with self.lock:
				runFlag = self.runFlag
コード例 #18
0
def locate_thumbnail(thumbnail_filename, source_filename, display=False, save_visualization=False,
                     save_reconstruction=False, reconstruction_format="jpg"):
    thumbnail_basename, thumbnail_image = open_image(thumbnail_filename)
    source_basename, source_image = open_image(source_filename)

    logging.info("Attempting to locate %s within %s", thumbnail_filename, source_filename)
    kp_pairs = match_images(thumbnail_image, source_image)

    if len(kp_pairs) >= 4:
        title = "Found %d matches" % len(kp_pairs)
        logging.info(title)

        H, mask = find_homography(kp_pairs)

        new_thumbnail, corners, rotation = reconstruct_thumbnail(thumbnail_image, source_image, kp_pairs, H)

        print(json.dumps({
            "master": {
                "source": source_filename,
                "dimensions": {
                    "height": source_image.shape[0],
                    "width": source_image.shape[1],
                }
            },
            "thumbnail": {
                "source": thumbnail_filename,
                "dimensions": {
                    "height": thumbnail_image.shape[0],
                    "width": thumbnail_image.shape[1],
                }
            },
            "bounding_box": {
                "height": corners[0][1] - corners[0][0],
                "width": corners[1][1] - corners[1][0],
                "x": corners[1][0],
                "y": corners[0][0],
            },
            "rotation_degrees": rotation
        }))

        if save_reconstruction:
            new_filename = "%s.reconstructed.%s" % (thumbnail_basename, reconstruction_format)
            cv2.imwrite(new_filename, new_thumbnail)
            logging.info("Saved reconstructed thumbnail %s", new_filename)
    else:
        logging.warning("Found only %d matches; skipping reconstruction", len(kp_pairs))
        new_thumbnail = corners = H = mask = None

    if display or save_visualization:
        vis_image = visualize_matches(source_image, thumbnail_image, new_thumbnail, corners, kp_pairs, mask)

    if save_visualization:
        vis_filename = "%s.visualized%s" % os.path.splitext(thumbnail_filename)
        cv2.imwrite(vis_filename, vis_image)
        logging.info("Saved match visualization %s", vis_filename)

    if display:
        cv2.imshow(title, vis_image)
        cv2.waitKey()
        cv2.destroyAllWindows()
コード例 #19
0
ファイル: Assignment2.py プロジェクト: MartinFaartoft/sigb
def showImageandPlot(N):
    #A simple attenmpt to get mouse inputs and display images using matplotlib
    I = cv2.imread('groundfloor.bmp')
    drawI = I.copy()
    #make figure and two subplots
    fig = figure(1)
    ax1  = subplot(1,2,1)
    ax2  = subplot(1,2,2)
    ax1.imshow(I)
    ax2.imshow(drawI)
    ax1.axis('image')
    ax1.axis('off')
    points = fig.ginput(5)
    fig.hold('on')

    for p in points:
        #Draw on figure
        subplot(1,2,1)
        plot(p[0],p[1],'rx')
        #Draw in image
        cv2.circle(drawI,(int(p[0]),int(p[1])),2,(0,255,0),10)
    ax2.cla
    ax2.imshow(drawI)
    draw() #update display: updates are usually defered
    show()
    savefig('somefig.jpg')
    cv2.imwrite("drawImage.jpg", drawI)
def img_test(forest, feature_extractor, points, colors, filename, size=512,
             radius=3, proba=True):
    img = np.zeros((size, size, 3))
    v_min = points.min()
    v_max = points.max()
    step = float(v_max - v_min) / img.shape[0]
    grid = np.arange(v_min, v_max, step)

    xy = np.array(list(itertools.product(grid, grid)))
    features = feature_extractor.apply_all(xy)

    if proba:
        r = forest.predict_proba(features)
        col = np.dot(r, colors)
    else:
        r = forest.predict(features).astype('int32')
        col = colors[r]
    img[((xy[:, 1] - v_min) / step).astype('int32'),
        ((xy[:, 0] - v_min) / step).astype('int32')] = col

    points = ((points - v_min) / step).astype('int')
    for p, r in zip(points, responses):
        col = tuple(colors[int(r)])
        cv2.circle(img, tuple(p), radius + 1, (0, 0, 0), thickness=-1)
        cv2.circle(img, tuple(p), radius, col, thickness=-1)

    cv2.imwrite(filename, img)
コード例 #21
0
def show_circle(img, img_file_name):
    temp_img = cv2.medianBlur(img, 5)
    c_img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
    circles = find_circle(temp_img, 10, 100, 30)
    ix, iy = 0, 0

    if circles is None:
        circle_out_str = "_circle_%d-%d.png" % (ix, iy)
        circle_out_file = re.sub(r'\.jpg', circle_out_str, img_file_name)
        cv2.imwrite(circle_out_file, c_img)
        return False

    for i in circles[0, :]:
        center = (i[0], i[1])
        ix, iy = i[0], i[1]
        radius = i[2]
        circle_color = (0, 255, 0)
        cv2.circle(c_img, center, radius, circle_color, 1)
        center_color = (0, 0, 255)
        cv2.circle(c_img, center, 2, center_color, 1)

    circle_out_str = "_circle_%d-%d.png" % (ix, iy)
    circle_out_file = re.sub(r'\.jpg', circle_out_str, img_file_name)
    cv2.imwrite(circle_out_file, c_img)
    return True
コード例 #22
0
    def show_results(self, image, results, imshow=True, deteted_boxes_file=None,
                     detected_image_file=None):
        """Show the detection boxes"""
        img_cp = image.copy()
        if deteted_boxes_file:
            f = open(deteted_boxes_file, "w")
        #  draw boxes
        for i in range(len(results)):
            x = int(results[i][1])
            y = int(results[i][2])
            w = int(results[i][3]) // 2
            h = int(results[i][4]) // 2
            if self.verbose:
                print("   class: %s, [x, y, w, h]=[%d, %d, %d, %d], confidence=%f" % (results[i][0],
                            x, y, w, h, results[i][-1]))

                cv2.rectangle(img_cp, (x - w, y - h), (x + w, y + h), (0, 255, 0), 2)
                cv2.rectangle(img_cp, (x - w, y - h - 20), (x + w, y - h), (125, 125, 125), -1)
                cv2.putText(img_cp, results[i][0] + ' : %.2f' % results[i][5], (x - w + 5, y - h - 7),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1)
            if deteted_boxes_file:
                f.write(results[i][0] + ',' + str(x) + ',' + str(y) + ',' +
                        str(w) + ',' + str(h)+',' + str(results[i][5]) + '\n')
        if imshow:
            cv2.imshow('YOLO_small detection', img_cp)
            cv2.waitKey(1)
        if detected_image_file:
            cv2.imwrite(detected_image_file, img_cp)
        if deteted_boxes_file:
            f.close()
コード例 #23
0
ファイル: tests.py プロジェクト: piratefsh/set-solver
def test():
    # 3 cards on flat table
    cards_3 = cv2.imread('images/set-3-texture.jpg')

    # 5 cards at an angle
    cards_5 = cv2.imread('images/set-5-random.jpg')

    thresh_3 = s.get_binary(cards_3)
    contours = s.find_contours(thresh_3, 3)

    assert len(s.transform_cards(cards_3, contours, 3)) == 3

    res5 = s.detect_cards(cards_5)
    assert res5 is not None and len(res5) == 5

    res3 = s.detect_cards(cards_3)
    assert res3 is not None and len(res3) == 3

    for i in range(len(res5)):
        c = res5[i]
        # util.show(c, 'card')
        cv2.imwrite('images/cards/card-5-%d.jpg' % i, c)

    for i in range(len(res3)):
        c = res3[i]
        # util.show(c, 'card')
        cv2.imwrite('images/cards/card-3-%d.jpg' % i, c)

    # for cards detected, get properties
    for link in os.listdir('images/cards'):
        img = cv2.imread('images/cards/%s' % link)
        test_props(img)
    print 'tests pass'
コード例 #24
0
def det2negative(xmldoc, opath):
    samples = xmldoc.getElementsByTagName('sample')
    for sample in samples:
        detections = sample.getElementsByTagName('detections')
        detections = minidom.parseString(detections[0].toxml())
        detections = detections.getElementsByTagName("_")
        if len(detections) is not 0:
            path = sample.getElementsByTagName("path")
            path = path[0].firstChild.nodeValue
            mat = cv2.imread(path)
            mat_h, mat_w, _ = mat.shape

            for detection in detections:
                detection = detection.childNodes
                for each in detection:
                    rect = eval(re.sub( r"\b\s\b", ",", re.sub(r"\n", "[", each.nodeValue )) + "]")
                    print rect

                    ratio = 64.0 / rect[3]

                    print rect, ratio
                    mat = resize(mat, int(round(mat_w * ratio)), int(round(mat_h * ratio)))

                    rect[0] = int(round(ratio * rect[0])) - 10
                    rect[1] = int(round(ratio * rect[1])) - 10
                    rect[2] = rect[0] + 32 + 20
                    rect[3] = rect[1] + 64 + 20
                    try:
                        cropped = mat[rect[1]:(rect[3]), rect[0]:(rect[2]), :]
                        img = os.path.join(opath, ''.join(random.choice(string.lowercase) for i in range(8)) + ".png")
                        cr_h, cr_w, _ = cropped.shape
                        if cr_h is 84 and cr_w is 52:
                            cv2.imwrite(img, cropped)
                    except:
                        pass
コード例 #25
0
def listener():
    global fnobj
    rospy.init_node('reconocimiento', anonymous=True) 
    rospy.Subscriber("chatter", coordenadas, callback)
    
    rate = rospy.Rate(50) #hz
    cap = cv2.VideoCapture(0)
    fnobj = 'logo.png'
    opts, args = getopt.getopt(sys.argv[1:], '', ['feature='])
    opts = dict(opts)
    feature_name = opts.get('--feature', 'sift')

    while not rospy.is_shutdown():
  
        ret, frame = cap.read()
    	crop_img = frame[y:h,x:w]   
    	cv2.imwrite("full.png",crop_img)
	
	
        img1 = cv2.imread('full.png',0)
        img2 = cv2.imread(fnobj, 0)
        detector, matcher = init_feature(feature_name)
	if detector != None:
        	print 'usando', feature_name
        else:
            	print 'unknown feature:', feature_name
            	sys.exit(1)

        kp1, desc1 = detector.detectAndCompute(img1, None)
        kp2, desc2 = detector.detectAndCompute(img2, None)
        print 'img1 - %d features, img2 - %d features' % (len(kp1), len(kp2))
	
	match_and_draw('analisis', matcher,desc1,desc2,kp1,kp2,img1,img2)	
	cv2.waitKey(1)
コード例 #26
0
ファイル: start.py プロジェクト: KaiHangYang/calMethod
    def _save(self, *argv):
        if not self._preview(): return
        self.consoleNor("过程控制:图片正在处理中...")
        filepath = tkFileDialog.asksaveasfilename()

        filename = os.path.basename(filepath)
        dotNum = filename.count(".")

        if dotNum == 0:
            if filename == "" : return
            postfix = ".jpg"
        elif dotNum >= 2 or not (filename.split(".")[1] in IMAGE_TYPES):
            self.consoleErr("格式错误:文件名格式错误!")
            return
        else:
            postfix = ""

        if os.path.exists(self.filename) and fileCheck.isImage(self.filename):
            try:
                if self.algNum == 0:
                    im = tool_cv2.resize_linear(self.filename, self.wScale, self.hScale)
                else:
                    im = tool_cv2.resize_cubic(self.filename, self.wScale, self.hScale)
                self.consoleNor("过程控制:图片处理完毕,开始保存...")
                cv2.imwrite(filepath+postfix, im)
                self.consoleNor("过程控制:图片保存完毕...")
            except:
                self.consoleErr("过程控制:图片保存过程出现问题...")
        else:
            self.consoleErr("类型错误:打开的不是图片或者是文件不存在!")
コード例 #27
0
def evaluate(im, algo, gt_illuminant, i, range_thresh, bin_num, dst_folder):
    new_im = None
    start_time = timeit.default_timer()
    if algo=="grayworld":
        new_im = cv2.xphoto.autowbGrayworld(im, 0.95)
    elif algo=="nothing":
        new_im = im
    elif algo=="learning_based":
        new_im = cv2.xphoto.autowbLearningBased(im, None, range_thresh, 0.98, bin_num)
    elif algo=="GT":
        gains = gt_illuminant / min(gt_illuminant)
        g1 = float(1.0 / gains[2])
        g2 = float(1.0 / gains[1])
        g3 = float(1.0 / gains[0])
        new_im = cv2.xphoto.applyChannelGains(im, g1, g2, g3)
    time = 1000*(timeit.default_timer() - start_time) #time in ms

    if len(dst_folder)>0:
        if not os.path.exists(dst_folder):
            os.makedirs(dst_folder)
        im_name = ("%04d_" % i) + algo + ".jpg"
        cv2.imwrite(os.path.join(dst_folder, im_name), stretch_to_8bit(new_im))

    #recover the illuminant from the color balancing result, assuming the standard model:
    estimated_illuminant = [0, 0, 0]
    eps = 0.01
    estimated_illuminant[2] = np.percentile((im[:,:,0] + eps) / (new_im[:,:,0] + eps), 50)
    estimated_illuminant[1] = np.percentile((im[:,:,1] + eps) / (new_im[:,:,1] + eps), 50)
    estimated_illuminant[0] = np.percentile((im[:,:,2] + eps) / (new_im[:,:,2] + eps), 50)

    res = np.arccos(np.dot(gt_illuminant,estimated_illuminant)/
                   (np.linalg.norm(gt_illuminant) * np.linalg.norm(estimated_illuminant)))
    return (time, (res / np.pi) * 180)
コード例 #28
0
ファイル: imagecatldr.py プロジェクト: nayaneye/FueraEye
def camactivate ():

	with picamera.PiCamera() as camera:
		camera.resolution = (512,512)
		time.sleep(2)
		camera.capture('im1.jpg')
		time.sleep(2)
		camera.capture('im2.jpg')
		time.sleep(2)
		camera.capture('im3.jpg')
		time.sleep(2)
		camera.capture('im4.jpg')

	im1=cv2.imread('im1.jpg',1)
	im2=cv2.imread('im2.jpg',1)
	im3=cv2.imread('im3.jpg',1)
	im4=cv2.imread('im4.jpg',1)

	cv2.putText(im1,'Cam1',(10,20),cv2.FONT_HERSHEY_SIMPLEX,1,(255,255,255),2)
	cv2.putText(im2,'Cam2',(10,20),cv2.FONT_HERSHEY_SIMPLEX,1,(255,255,255),2)
	cv2.putText(im3,'Cam3',(10,20),cv2.FONT_HERSHEY_SIMPLEX,1,(255,255,255),2)
	cv2.putText(im4,'Cam4',(10,20),cv2.FONT_HERSHEY_SIMPLEX,1,(255,255,255),2)


	cv2.namedWindow('Catenated Images',cv2.WINDOW_NORMAL)
	concat=np.zeros((1024,1024,3),np.uint8)
	concat[0:512,0:512,:]=im1
	concat[0:512,512:1024,:]=im2
	concat[512:1024,0:512,:]=im3
	concat[512:1024,512:1024,:]=im4

	cv2.imshow('Catenated Images',concat)
	cv2.imwrite('concat.jpg',concat)
	cv2.waitKey(0)
コード例 #29
0
ファイル: cell.py プロジェクト: treeherder/computer_vision
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)
コード例 #30
0
def _generate_training_set(img, image_file):
    save_location = "images/training/"
    _, img = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY_INV)
    _, regions = cv2.connectedComponents(img, img)

    if not os.path.exists("../images/cc"):
        os.makedirs("../images/cc")

    cv2.imwrite("../images/cc/cc.png", regions)
    cc = cv2.imread("../images/cc/cc.png", 0)
    _, cc_vis = cv2.threshold(cc, 1, 255, cv2.THRESH_BINARY)

    _, contours, hierarchy = cv2.findContours(cc_vis, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    idx = 0
    for cnt in contours:
        area = cv2.contourArea(cnt)
        if area < 50 or area > 1000:
            continue
        if len(cnt) < 5:
            continue
        idx += 1
        x, y, w, h = cv2.boundingRect(cnt)
        roi = img[y: y + h, x: x + w]
        name = image_file.split('.')[0]
        inverted = (255 - roi)
        cv2.imwrite(save_location + name + str(idx) + '.jpg', inverted)
    cv2.waitKey(0)
コード例 #31
0
            # Label as found be YOLO CNN
            label = str(category.decode("utf-8"))

            # Draw Bounding Boxes around found RoIs, with different colours depending on found label/class
            if label == "horse":
                cv2.rectangle(img, (int(x - w / 2), int(y - h / 2)), (int(x + w / 2), int(y + h / 2)), (255, 0, 0),
                              thickness=2)
            if label == "horse":
                cv2.rectangle(img, (int(x - w / 2), int(y - h / 2)), (int(x + w / 2), int(y + h / 2)), (0, 0, 255),
                              thickness=2)

            # Write label and confidence score above each BoundingBox
            cv2.putText(img, f"{label} ({score})", (int(x), int(y)), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 0))

        # Save image
        cv2.imwrite("out.jpg", img)

    # flag to label an entire video set (-v)
    elif sys.argv[1] == "-v":
        # video passed as path in arguments (argv[2])
        filepath = sys.argv[2]
        # read video
        vid = cv2.VideoCapture(filepath)

        try:
            # Check if video could be opened; if yes, process it
            if vid.isOpened():
                # Find OpenCV version, to determine parameters that have been changed between versions
                (major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.')

                if int(major_ver) < 3:
import cv2
import numpy as np

img = cv2.imread(r'C:\Users\tchat\.spyder-py3\bumblebee.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

sift = cv2.xfeatures2d.SIFT_create()
kp = sift.detect(gray, None)

img = cv2.drawKeypoints(img,
                        kp,
                        None,
                        flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
cv2.imwrite('D:\Image Processing\HW3\sift_bum_keypoints.jpg', img)

surf = cv2.xfeatures2d.SURF_create(4000)
(kps, descs) = surf.detectAndCompute(gray, None)

img1 = cv2.drawKeypoints(gray,
                         kps,
                         None,
                         flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
cv2.imwrite('D:\Image Processing\HW3\surf_bum_keypoints.jpg', img1)
コード例 #33
0
ファイル: skeleton.py プロジェクト: ContentMine/CMForestPlots
    def __init__(self, image_directory):

        print(image_directory)

        img = cv2.imread(os.path.join(image_directory, "raw.png"))

        self.height, self.width = img.shape[0:2]

        self.horizontal_lines = []
        self.vertical_lines = []

        low_threshold = 100
        high_threshold = 150
        #edges = cv2.Canny(img, low_threshold, high_threshold)
        edges = auto_canny(img)

        cv2.imwrite("/tmp/edges.png", edges)

        rho = 1  # distance resolution in pixels of the Hough grid
        theta = np.pi / 180  # angular resolution in radians of the Hough grid
        threshold = 15  # minimum number of votes (intersections in Hough grid cell)
        min_line_length = 150  # minimum number of pixels making up a line
        max_line_gap = 30  # maximum gap in pixels between connectable line segments
        line_image = np.copy(img) * 0  # creating a blank to draw lines on

        # Run Hough on edge detected image
        # Output "lines" is an array containing endpoints of detected line segments
        lines = cv2.HoughLinesP(edges, rho, theta, threshold, np.array([]),
                                min_line_length, max_line_gap)

        try:
            vertical_lines = [x for x in lines if x[0][0] == x[0][2]]
        except TypeError:
            return

        # debounce vertical_lines lines
        vertical_lines.sort(key=lambda a: a[0][0])
        clean = vertical_lines[:1]
        for i in range(len(vertical_lines) - 1):
            line = vertical_lines[i + 1]
            last = clean[-1]
            if abs(last[0][0] - line[0][0]) < 20:
                # two very close line, just keep the longest
                last_len = last[0][1] - last[0][3]
                line_len = line[0][1] - line[0][3]
                if line_len > last_len:
                    clean = clean[:-1]
                    clean.append(line)
            else:
                clean.append(line)
        vertical_lines = clean

        # sort by size, biggest to smallest
        vertical_lines.sort(key=lambda a: a[0][1] - a[0][3], reverse=True)

        try:
            main_line = vertical_lines[0]
        except IndexError:
            return
        self.vertical_lines = [
            VerticalLine(a[0][0], a[0][3], a[0][1]) for a in vertical_lines[:2]
        ]

        # filter: y1 == y2 and x1 < main_line.x1 and y2 > main_line.y1  - and y1 > main_line.y1 and y1 < main_line.y2
        horizontal_lines = [
            a for a in lines
            if a[0][1] == a[0][3] and (a[0][0] < main_line[0][0] < a[0][2]) and
            (main_line[0][3] <= a[0][3] <= main_line[0][1])
        ]

        # debounce horizontal_lines
        horizontal_lines.sort(key=lambda a: a[0][1])
        clean = horizontal_lines[:1]
        for i in range(len(horizontal_lines) - 1):
            line = horizontal_lines[i + 1]
            last = clean[-1]
            if abs(last[0][1] - line[0][1]) < 20:
                # two very close line, just keep the longest
                last_len = last[0][2] - last[0][0]
                line_len = line[0][2] - line[0][0]
                if line_len > last_len:
                    clean = clean[:-1]
                    clean.append(line)
            else:
                clean.append(line)
        horizontal_lines = clean

        # now filter the horizontal lines, keeping the ones at the clostest to top and bottom
        try:
            top_line = horizontal_lines[0]
            bottom_line = horizontal_lines[-1]
            self.horizontal_lines = [
                HorizontalLine(x[0][1], x[0][0], x[0][2])
                for x in [top_line, bottom_line]
            ]
        except IndexError:
            self.horizontal_lines = []

#
#         horizontal_lines.sort(key=lambda a: a[0][2] - a[0][0], reverse=True)
#         horizontal_lines = horizontal_lines[:2]
#         horizontal_lines.sort(key=lambda a: a[0][1])

# Find the longest vertical line_image - ideally just one
        for vline in self.vertical_lines:
            _ = cv2.line(line_image, (vline.x, vline.y1), (vline.x, vline.y2),
                         (128 + int(random.random() * 128), 128, 128), 1)

        for hline in self.horizontal_lines:
            _ = cv2.line(line_image, (hline.x1, hline.y), (hline.x2, hline.y),
                         (128, 128 + int(random.random() * 128), 128), 1)
        #
        #
        # lines_edges = cv2.addWeighted(img, 0.8, line_image, 1, 0)

        cv2.imwrite(os.path.join(image_directory, "lines.png"), line_image)
コード例 #34
0
def compute_disparity_from_z_info(
        filepath_in: str,
        filepath_out: str,
        baseline_mm: float,
        calibration_matrix: np.array,
        res_x: int = bpy.context.scene.render.resolution_x,
        res_y: int = bpy.context.scene.render.resolution_y,
        scale: float = 1e4):
    """Compute disparity map from given z info (depth or range). Values are in .1 mm
    By convention, disparity is computed from left camera to right camera (even for the right camera).

    if z = depth, this is assumed to be stored as a PNG image of uint16 (compressed) values in .1 mm
    If z = range, this is assumed to be stored as a EXR image of float32 (true range) values in meters

    Args:
        fpath_in(str): path to input file to read in
        fpath_out(str): path to output file to write out. If None (explicitly given), only return (no save to file).
                        NOTE: make sure the filesystem tree exists
        baseline_mm(float): baseline value (in mm) between parallel cameras setup
        calibration_matrix(np.array): 3x3 camera calibration matrix. Used also to extract the focal lenght in pixel

    NOTE: if z = range, depth must be computed. This requires additional arguments.
          See  amira_blender_rendering.utils.camera.project_pinhole_range_to_rectified_depth

    Opt Args:
        res_x(int): render/image x resolution. Default: bpy.context.scene.resolution_x
        res_y(int): render/image y resolution. Default: bpy.context.scene.resolution_y
        scale(float): value used to convert range (in m) to depth. Default: 1e4 (.1mm)

    Returns:
        np.array<np.uint16>: disparity map in pixels
    """
    # check filepath_in to read file from
    if '.png' in filepath_in:
        logger.info(f'Loading depth from .PNG file {filepath_in}')
        depth = (cv2.imread(filepath_in,
                            cv2.IMREAD_UNCHANGED)).astype(np.uint16)

    elif '.exr' in filepath_in:
        # in case of exr file we convert range to depth first
        logger.info(f'Computing depth from EXR range file {filepath_in}')
        depth = project_pinhole_range_to_rectified_depth(
            filepath_in, None, calibration_matrix, res_x, res_y, scale)

    else:
        logger.error(
            f'Given file {filepath_in} is neither of type PNG nor EXR. Skipping!'
        )
        return

    # depth is always converted to mm and to float for precision computations
    depth = depth.astype(np.float32) / (scale / 1e3)

    logger.info('Computing disparity map')
    # get focal lenght
    focal_length_px = calibration_matrix[0, 0]
    # disparity in pixels
    disparity = baseline_mm * focal_length_px * np.reciprocal(depth)
    # cast to uint16 for PNG format
    disparity = (disparity).astype(np.uint16)

    # write out if requested
    if filepath_out is not None:
        cv2.imwrite(
            filepath_out, disparity,
            [cv2.IMWRITE_PNG_STRATEGY, cv2.IMWRITE_PNG_STRATEGY_DEFAULT])
        logger.info(f'Saved disparity map at {filepath_out}')

    return disparity
コード例 #35
0
def project_pinhole_range_to_rectified_depth(
        filepath_in: str,
        filepath_out: str,
        calibration_matrix: np.array,
        res_x: int = bpy.context.scene.render.resolution_x,
        res_y: int = bpy.context.scene.render.resolution_y,
        scale: float = 1e4):
    """
    Given a depth map computed (as standard in ABR setup) using a perfect pinhole model,
    compute the projected rectified depth

    The function assumes to read-in from an Exr image format (32 bit true range values)
    and to write-out in PNG image format (16 bit truncated depth values).

    NOTE: depth values that might cause overflow in 16bit (i.e. >65k) are set to 0.
    
    Args:
        filepath_in(str): path to (.exr) file with range values (assumed in m) to load
        filepath_out(str): path to (.png) file where (rectified) depth map is write to.
                           If None (explicitly given) only return converted image (no save).
                           NOTE: make sure the filesystem tree exists
        calibration_matrix(np.array): 3x3 camera calibration matrix

    Optional Args:
        res_x(int): render/image x resolution (pixel). Default is initial scene resolution_x
        res_y(int): render/image y resolution (pixel). Default is initial scene resolution_y
        scale(float): scaling factor to convert range (in m) to depth. Default 1e4 (m to .1 mm)

    Return:
        np.array<np.uint16>: converted depth
    """
    # quick check file type
    if '.exr' not in filepath_in:
        raise ValueError(f'Given input file {filepath_in} not of type EXR')
    if '.png' not in filepath_out and filepath_out is not None:
        raise ValueError(f'Given output file {filepath_out} not of tyep PNG')

    # read range image (float32 values) in meters
    # NOTE: the ANYDEPTH flag lead to a offset in the read value
    # range_exr = (cv2.imread(filepath_in, cv2.IMREAD_ANYDEPTH)).astype(np.float32)
    range_exr = (cv2.imread(filepath_in,
                            cv2.IMREAD_UNCHANGED))[:, :, 0].astype(np.float32)

    logger.info('Rectifying pinhole range map into depth')
    grid = np.indices((res_y, res_x))
    u = grid[1].flatten()
    v = grid[0].flatten()
    uv1 = np.array([u, v, np.ones(res_x * res_y)])

    K_inv = np.linalg.inv(calibration_matrix)
    v_dirs_mtx = np.dot(K_inv, uv1).T.reshape(res_y, res_x, 3)
    v_dirs_mtx_unit_inv = np.reciprocal(np.linalg.norm(v_dirs_mtx, axis=2))

    depth_img = (range_exr * v_dirs_mtx_unit_inv * scale)
    # remove overflow values
    depth_img[depth_img > 65000] = 0
    # cast to 16 bit
    depth_img = depth_img.astype(np.uint16)

    # write out if requested
    if filepath_out is not None:
        cv2.imwrite(
            filepath_out, depth_img,
            [cv2.IMWRITE_PNG_STRATEGY, cv2.IMWRITE_PNG_STRATEGY_DEFAULT])
        logger.info(f'Saved (rectified) depth map at {filepath_out}')

    return depth_img
コード例 #36
0
def face_detection(DONE, saved_frames_counter, max_saved_frames):
    cap = cv2.VideoCapture(2)

    detector = dlib.get_frontal_face_detector()
    predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")

    x_ratio = 0.3
    y_ratio = 0.5

    frame_id = 0
    save_frame_path = "./face_frames/"  # overwrite previus images
    frame_container = [None] * max_saved_frames
    frame_container_coords = [None] * max_saved_frames
    while True:
        _, frame = cap.read()

        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        faces = detector(gray)

        center = np.zeros(2, dtype=int)
        x_Crop = 0
        y_Crop = 0
        save = False
        for face in faces:
            x1 = face.left()
            y1 = face.top()
            x2 = face.right()
            y2 = face.bottom()
            # cv2.rectangle(frame, (x1,y1),(x2,y2),(0,255,0),3)

            y_Crop = int((x2 - x1) * (1.0 + x_ratio)) // 2
            x_Crop = int((y2 - y1) * (1.0 + y_ratio)) // 2

            landmarks = predictor(gray, face)

            center = np.zeros(2, dtype=int)
            for n in range(0, 68):
                x = landmarks.part(n).x
                y = landmarks.part(n).y
                # cv2.circle(frame,(x,y),4,(255,0,0),-1)
                center += np.array([x, y])

            center = center // 68

            save = random.random() > 0.5

        # print(center)
        # cv2.circle(frame, (center[0],center[1]), 4, (0,0,255), -1)

        frame_coords = [
            center[1] - x_Crop, center[1] + x_Crop, center[0] - y_Crop,
            center[0] + y_Crop
        ]
        cv2.imshow("LandMark Detection", frame)
        if save and saved_frames_counter < max_saved_frames:
            frame_container[saved_frames_counter] = copy.copy(frame)
            frame_container_coords[saved_frames_counter] = copy.copy(
                frame_coords)
            saved_frames_counter += 1

        if saved_frames_counter == max_saved_frames and not DONE:
            print("The Coords")
            shape = np.matrix(frame_container_coords).sum(
                axis=0) // max_saved_frames
            shape = shape.tolist()[0]
            print(shape)
            for ii in range(max_saved_frames):
                alt_frame = frame_container[ii][shape[0]:shape[1],
                                                shape[2]:shape[3], :]
                cv2.imwrite(save_frame_path + "frame{:d}.jpg".format(ii),
                            alt_frame)
                print("Working" + "".join(["."] * (ii % 4)))
            print("DONE")
            DONE = True

        key = cv2.waitKey(1)
        if key == 27:
            break

        frame_id += 1

    cv2.destroyAllWindows()
    cap.release()
    im = img.crop((x, y, x + nw, y + nh))
    return im, x, y, x + nw, y + nh


#Width and height of random crops
widthc = 1000
heightc = 700

i = 0
listFiles = os.listdir('.')
pattern = "*.png"
print(listFiles)
for entry in listFiles:
    if fnmatch.fnmatch(entry, pattern):
        #Resize image before performing random crops
        im = Image.open(entry.format(i + 1))
        im = im.resize((2707, 3828), Image.BILINEAR)

        for j in range(0, numcrops):
            image, xminc, yminc, xmaxc, ymaxc = randCrop(im, widthc, heightc)
            imcv2 = np.array(image)
            togray = cv2.cvtColor(imcv2, cv2.COLOR_RGB2GRAY)
            image2 = np.zeros_like(image)
            image2[:, :, 0] = togray
            image2[:, :, 1] = togray
            image2[:, :, 2] = togray
            cv2.imwrite(
                'newcrops/randscore{0}'.format(i + 1) +
                'crop{0}.jpg'.format(j + 1), image2)
        i += 1
コード例 #38
0
ファイル: capture.py プロジェクト: AntonioNvs/Dashboard
    # Detectando as faces na imagem
    detecteds_face = classificator.detectMultiScale(image_gray,
                                                    scaleFactor=1.5,
                                                    minSize=(150, 150))

    # Percorrendo todas as faces detectadas
    for (x, y, l, a) in detecteds_face:
        # Desenhando um retângulo sobre a face
        cv2.rectangle(image, (x, y), (x + l, y + a), (0, 0, 255), 2)

        # Caso aperte 'c', é tirado uma foto para análise
        if cv2.waitKey(1) & 0xFF == ord('c'):
            # Recortando somente o rosto
            image_face = cv2.resize(image_gray[y:y + a, x:x + l], (220, 220))

            # Escrevendo a imagem
            cv2.imwrite(
                "Pictures/pessoa." + str(id) + "." + str(image) + ".jpg",
                image_face)
            print("[Foto " + str(sample) + " capturada com sucesso]")
            sample += 1

    cv2.imshow("Face", image)
    cv2.waitKey(1)
    if sample >= number_samples + 1:
        break

print("Faces capturadas com sucesso")
web_camera.release()
cv2.destroyAllWindows()
コード例 #39
0
import time
import subprocess
import cv2

def detect_gender(img_path):
    output = subprocess.check_output('th gender_detection.lua '+img_path, shell=True)
    gender = output.split('\n')[-2].split('\t')[0]
    return gender


if __name__ == "__main__":
    img_path = "tmp.jpg"

    #Starting the camera
    vid = cv2.VideoCapture(0)

    while(vid.isOpened()):
        start_time = time.time()
        ret, frame = vid.read()

        if ret==True:
            cv2.imwrite("tmp.jpg",frame)
            gender = detect_gender(img_path)
            print gender
            if gender == 'Male' or gender == 'Female':
                cv2.putText(frame,gender,(100,100), cv2.FONT_HERSHEY_SIMPLEX, 1,(0,255,0),2)
            print("Time taken = ",time.time()-start_time)
        cv2.imshow("Gender",frame)
        cv2.waitKey(1)
コード例 #40
0
ファイル: main.py プロジェクト: lobbie/mAP
          color = green
        text = "Result: " + status + " "
        img, line_width = draw_text_in_image(img, text, (margin + line_width, v_pos), color, line_width)

        if ovmax > 0: # if there is intersections between the bounding-boxes
          bbgt = [ float(x) for x in gt_match["bbox"].split() ]
          cv2.rectangle(img,(int(bbgt[0]),int(bbgt[1])),(int(bbgt[2]),int(bbgt[3])),light_blue,2)
        if status == "MATCH!":
          cv2.rectangle(img,(int(bb[0]),int(bb[1])),(int(bb[2]),int(bb[3])),green,2)
        else:
          cv2.rectangle(img,(int(bb[0]),int(bb[1])),(int(bb[2]),int(bb[3])),light_red,2)
        cv2.imshow("Animation", img)
        cv2.waitKey(20) # show image for 20 ms
        # save image to results
        output_img_path = results_files_path + "/images/" + class_name + "_prediction" + str(idx) + ".jpg"
        cv2.imwrite(output_img_path, img)

    #print(tp)
    # compute precision/recall
    cumsum = 0
    for idx, val in enumerate(fp):
      fp[idx] += cumsum
      cumsum += val
    cumsum = 0
    for idx, val in enumerate(tp):
      tp[idx] += cumsum
      cumsum += val
    #print(tp)
    rec = tp[:]
    for idx, val in enumerate(tp):
      rec[idx] = float(tp[idx]) / gt_counter_per_class[class_name]
コード例 #41
0
        out1 = conv(dma,xlnk,in_buffer,1,0,precision,img,B1,W1,Configuration,in_buffer_x,in_buffer_bias,in_buffer_weight,in_buffer_param,in_buffer_out)
        # test =np.divide(out1,precision).reshape(32,32,32)
        # print(test[:,:,0])
        out2 = conv(dma,xlnk,in_buffer,1,0,precision,out1,B2,W2,Configuration,in_buffer_x,in_buffer_bias,in_buffer_weight,in_buffer_param,in_buffer_out)
        # test =np.divide(out2,precision).reshape(32,32,32)
        # print(test[:,:,0])
        out3 = conv(dma,xlnk,in_buffer,1,0,precision,out2,B3,W3,Configuration,in_buffer_x,in_buffer_bias,in_buffer_weight,in_buffer_param,in_buffer_out)
        # test =np.divide(out3,precision).reshape(32,32,32)
        # print(test[:,:,0])
        out4 = conv(dma,xlnk,in_buffer,1,0,precision,out3,B4,W4,Configuration,in_buffer_x,in_buffer_bias,in_buffer_weight,in_buffer_param,in_buffer_out)
        # test =np.divide(out4,precision).reshape(32,32,32)
        # print(test[:,:,0])
        out5 = conv(dma,xlnk,in_buffer,1,0,precision,out4,B5,W5,Configuration,in_buffer_x,in_buffer_bias,in_buffer_weight,in_buffer_param,in_buffer_out)
        out6 = conv(dma,xlnk,in_buffer,1,0,precision,out5,B6,W6,Configuration,in_buffer_x,in_buffer_bias,in_buffer_weight,in_buffer_param,in_buffer_out)
        out7 = conv(dma,xlnk,in_buffer,1,0,precision,out6,B7,W7,Configuration,in_buffer_x,in_buffer_bias,in_buffer_weight,in_buffer_param,in_buffer_out)
        out8 = conv(dma,xlnk,in_buffer,1,0,precision,out7,B8,W8,Configuration,in_buffer_x,in_buffer_bias,in_buffer_weight,in_buffer_param,in_buffer_out)
        out9 = conv(dma,xlnk,in_buffer,1,0,precision,out8,B9,W9,Configuration,in_buffer_x,in_buffer_bias,in_buffer_weight,in_buffer_param,in_buffer_out)
        # test =np.divide(out9,precision).reshape(32,32,32)
        # print(test[:,:,0])
        out = conv(dma,xlnk,in_buffer,0,1,precision,out9,B10,W10,Configuration,in_buffer_x,in_buffer_bias,in_buffer_weight,in_buffer_param,in_buffer_out)
        final = np.divide(out,precision).reshape(32,32,32)
        img_final[inputsize*h:inputsize*(h+1),inputsize*w:inputsize*(w+1)] = final[:,:,0]
t3 = time.time()
print(t3-t1)
x = sigmoid_all(img_final,256,256)  
final = img1*x+img2*(1-x)
# conv(dma,xlnk,in_buffer,relu,sigmoid,x,Bias,Weight,Configuration,in_buffer_x,in_buffer_bias,in_buffer_weight,in_buffer_param,in_buffer_out):
t2 = time.time()
print(t2-t1)
cv2.imwrite('./dma.jpg',x*255)
コード例 #42
0
ファイル: decoder.py プロジェクト: Gofinge/DMScanner
def dm_line_cycle_decoder(dm_image, output_dir, zxing_enable=True, **kwargs):
    img = cv2.GaussianBlur(dm_image.gray, (3, 3), 0)
    rets = dm_image.ret_candidate
    use_avaliable = np.zeros(len(rets))
    count = 0
    messages = []

    for ret in rets:
        show = False
        if use_avaliable[count] != 0:
            return
        use_avaliable[count] = 1
        pts1 = ret.astype(np.float32)
        width = int(
            np.sqrt((pts1[0][0] - pts1[1][0])**2 +
                    (pts1[0][1] - pts1[1][1])**2))
        height = int(
            np.sqrt((pts1[2][0] - pts1[1][0])**2 +
                    (pts1[2][1] - pts1[1][1])**2))
        aspect_ratio = float(width) / height
        if aspect_ratio < 1:
            width = min(256, max(128, width))
            height = int(width / aspect_ratio)
        else:
            height = min(256, max(128, height))
            width = int(height * aspect_ratio)
        # aspect_ratio = float(width)/height
        if height > width:
            aspect_ratio = 1 / aspect_ratio
        pts2 = np.float32([[0, 0], [width, 0], [width, height], [0, height]])
        M = cv2.getPerspectiveTransform(pts1, pts2)
        dst = cv2.warpPerspective(img, M, (width, height))
        search_range_w = width // 10
        search_width_w = max(4, search_range_w // 4)
        search_range_h = height // 10
        search_width_h = max(4, search_range_h // 4)

        internal_top, num_top = get_signal(dst[:search_range_w, :], width,
                                           search_width_w)
        internal_btm, num_btm = get_signal(dst[-search_range_w:, :], width,
                                           search_width_w)
        internal_lft, num_lft = get_signal(
            np.transpose(dst[:, :search_range_h]), height, search_width_h)
        internal_rht, num_rht = get_signal(
            np.transpose(dst[:, -search_range_h:]), height, search_width_h)

        four_egde = sorted([num_top, num_btm, num_lft, num_rht])
        sort_index = np.argsort(np.array([num_top, num_btm, num_lft, num_rht]))

        if aspect_ratio <= 1.6:
            # 方形重写。
            large_ud = max(num_top, num_btm)
            small_ud = min(num_top, num_btm)
            large_lr = max(num_lft, num_rht)
            small_lr = min(num_lft, num_rht)

            if (abs(large_ud - small_ud * 2) == 0
                    or abs(large_ud - small_lr * 2)
                    == 0) and large_ud >= 10:  # 上下最多跳跃点为正解时
                num = POSSIBLE_SQAR_SIZE[np.argmin(
                    abs(POSSIBLE_SQAR_SIZE - large_ud))]
            elif (abs(large_lr - small_lr * 2) == 0
                  or abs(large_lr - small_ud * 2)
                  == 0) and large_lr >= 10:  # 左右最多跳跃点为正解时
                num = POSSIBLE_SQAR_SIZE[np.argmin(
                    abs(POSSIBLE_SQAR_SIZE - large_lr))]
            elif (abs(large_ud - large_lr * 2) == 0
                  or abs(large_lr - large_ud * 2)
                  == 0) and max(large_lr, large_ud) >= 10:  # 存在双倍关系时
                num = POSSIBLE_SQAR_SIZE[np.argmin(
                    abs(POSSIBLE_SQAR_SIZE - max(large_ud, large_lr)))]
            else:
                if large_ud > large_lr:
                    index = np.argmin(
                        abs(np.concatenate((POSSIBLE_SQAR_SIZE - large_ud * 2, POSSIBLE_SQAR_SIZE - large_ud)))) % \
                            POSSIBLE_SQAR_SIZE.shape[0]
                    num = POSSIBLE_SQAR_SIZE[index]
                else:
                    index = np.argmin(
                        abs(np.concatenate((POSSIBLE_SQAR_SIZE - large_lr * 2, POSSIBLE_SQAR_SIZE - large_lr)))) % \
                            POSSIBLE_SQAR_SIZE.shape[0]
                    num = POSSIBLE_SQAR_SIZE[index]
            grid_num = [num, num]

        elif aspect_ratio > 1.6:
            # 矩形重写。
            # ud: up down
            # lr: left right
            large_ud = max(num_top, num_btm)
            small_ud = min(num_top, num_btm)
            large_lr = max(num_lft, num_rht)
            small_lr = min(num_lft, num_rht)
            if width > height:
                # #-------------#
                # |             |
                # #-------------#
                # 长边的点数量筛选。

                if abs(large_ud - small_ud * 2) == 0:
                    grid_num = POSSIBLE_RECT_SIZE[np.argmin(
                        abs(POSSIBLE_RECT_SIZE[:, 1] - large_ud))]

                elif abs(large_lr - small_lr * 2) == 0:
                    grid_num = POSSIBLE_RECT_SIZE[np.argmin(
                        abs(POSSIBLE_RECT_SIZE[:, 0] - large_lr))]

                else:  # 最差的结果:直接取长边。
                    index = np.argmin(abs(np.concatenate(
                        (POSSIBLE_RECT_SIZE[:, 1] - large_ud * 2, POSSIBLE_RECT_SIZE[:, 1] - large_ud)))) % \
                            POSSIBLE_RECT_SIZE.shape[0]
                    grid_num = POSSIBLE_RECT_SIZE[index]
            else:
                # #----#
                # |    |
                # |    |
                # |    |
                # |    |
                # #----#
                if abs(large_lr - small_lr * 2) == 0:
                    grid_num = POSSIBLE_RECT_SIZE[np.argmin(
                        abs(POSSIBLE_RECT_SIZE[:, 1] - large_lr))]

                elif abs(large_ud - small_ud * 2) == 0:
                    grid_num = POSSIBLE_RECT_SIZE[np.argmin(
                        abs(POSSIBLE_RECT_SIZE[:, 0] - large_ud))]

                else:  # 最差的结果:直接取长边。
                    index = np.argmin(abs(np.concatenate(
                        (POSSIBLE_RECT_SIZE[:, 1] - large_lr * 2, POSSIBLE_RECT_SIZE[:, 1] - large_lr)))) % \
                            POSSIBLE_RECT_SIZE.shape[0]
                    grid_num = POSSIBLE_RECT_SIZE[index]

                grid_num = [grid_num[1], grid_num[0]]

        inverse = False
        # draw a pseudo binary code.
        if min(grid_num) >= 4:
            grid_size_y = height / grid_num[0]
            grid_size_x = width / grid_num[1]
            y_offset = int(grid_size_y // 5)
            x_offset = int(grid_size_x // 5)
            res_img = np.zeros((8 * grid_num[0], 8 * grid_num[1]))
            sample_val = np.zeros((grid_num[0], grid_num[1]))

            # 取样开始。
            for i in range(grid_num[0]):
                for j in range(grid_num[1]):
                    sample_val[i][j] = np.mean(
                        dst[int(i * grid_size_y) +
                            y_offset:int((i + 1) * grid_size_y) - y_offset,
                            int(j * grid_size_x) +
                            x_offset:int((j + 1) * grid_size_x) - x_offset])

            mean_val = np.mean(sample_val)
            mean_border = (
                np.mean(sample_val[0, :]) + np.mean(sample_val[:, 0]) +
                np.mean(sample_val[-1, :]) + np.mean(sample_val[:, -1])) / 4
            # DOTO: 是否是dash需要判断。
            num_white_points = len(sample_val[0, :][sample_val[0, :] > mean_border]) + \
                               len(sample_val[:, 0][sample_val[:, 0] > mean_border]) + \
                               len(sample_val[-1, :][sample_val[-1, :] > mean_border]) + \
                               len(sample_val[:, -1][sample_val[:, -1] > mean_border])
            if num_white_points > grid_num[0] + grid_num[1]:
                inverse = True

            for i in range(grid_num[0]):
                for j in range(grid_num[1]):
                    interest_area = sample_val[i][j]
                    if np.mean(interest_area) > mean_val:
                        if not inverse:
                            res_img[i * 8:(i + 1) * 8, j * 8:(j + 1) * 8] = 255
                    elif inverse:
                        res_img[i * 8:(i + 1) * 8, j * 8:(j + 1) * 8] = 255
            # cv2.imwrite(out_dir + '/warp/init_res{}_{}.png'.format(os.path.basename(img_data.img_path), count), res_img)
            # refine finder boundary pattern.
            border_img = np.zeros_like(res_img)
            for i in range(1, grid_num[1], 2):
                border_img[-8:, i * 8:(i + 1) * 8] = 255
            for i in range(1, grid_num[0], 2):
                border_img[i * 8:(i + 1) * 8, -8:] = 255

            border_type = ['d', 'd', 'd', 'd']
            split_point = np.zeros(4)
            # top
            top_fake_border = res_img[0, :]
            split_point[0] = np.sum(
                np.abs(top_fake_border -
                       np.concatenate((res_img[0, 1:], np.zeros(1)))) / 255)
            # bottom
            btm_fake_border = res_img[-1, :]
            split_point[1] = np.sum(
                np.abs(btm_fake_border -
                       np.concatenate((res_img[-1, 1:], np.zeros(1)))) / 255)
            # left
            lft_fake_border = res_img[:, 0]
            split_point[2] = np.sum(
                np.abs(lft_fake_border -
                       np.concatenate((res_img[1:, 0], np.zeros(1)))) / 255)
            # right
            rht_fake_border = res_img[:, -1]
            split_point[3] = np.sum(
                np.abs(rht_fake_border -
                       np.concatenate((res_img[1:, -1], np.zeros(1)))) / 255)
            index = np.argsort(split_point)
            # print(np.argsort(split_point), split_point)

            border_type[index[0]] = 'l'
            border_type[index[1]] = 'l'
            if border_type[0] == border_type[3] == 'l':
                border_img = cv2.flip(border_img, 1)
            elif border_type[1] == border_type[2] == 'l':
                border_img = cv2.flip(border_img, 0)
            elif border_type[1] == border_type[3] == 'l':
                # print('trans')
                border_img = cv2.flip(border_img, 1)
                border_img = cv2.flip(border_img, 0)

            res_img[-8:, :] = border_img[-8:, :]
            res_img[:8, :] = border_img[:8, :]
            res_img[:, -8:] = border_img[:, -8:]
            res_img[:, :8] = border_img[:, :8]
            res_img = cv2.copyMakeBorder(res_img,
                                         50,
                                         50,
                                         50,
                                         50,
                                         cv2.BORDER_CONSTANT,
                                         value=255)
            # barcode = self.zxing_reader.decode()
        # draw some lines to justify the hypothesis.
        dst = cv2.cvtColor(dst, cv2.COLOR_GRAY2BGR)

        # print(output_dir + '/warp/{}_{}.png'.format(os.path.basename(dm_image.img_path), count))
        cv2.imwrite(
            output_dir + '/warp/{}_{}.png'.format(
                os.path.basename(dm_image.img_path), count), dst)
        if min(grid_num) >= 4:
            cv2.imwrite(
                output_dir + '/recon/{}_{}_code.png'.format(
                    os.path.basename(dm_image.img_path), count), res_img)
            cv2.imwrite(
                output_dir + '/mirror/{}_{}_code.png'.format(
                    os.path.basename(dm_image.img_path), count),
                cv2.flip(res_img, 0))
            if zxing_enable:
                message = zxing.BarCodeReader().decode(
                    output_dir + '/recon/{}_{}_code.png'.format(
                        os.path.basename(dm_image.img_path), count))
                if message.raw != '':
                    return message.raw
        count += 1
    return None
コード例 #43
0
######### drawing examples
fname='/data2/xuyangf/OcclusionProject/NaiveVersion/prunning/prunL3/dictionary_'+cat+'.pickle'
with open(fname,'rb') as fh:
    assignment, centers, example, __= pickle.load(fh)

ss=int(math.sqrt(example[0].shape[0]/3))

fname ='/data2/xuyangf/OcclusionProject/NaiveVersion/new_vc_score/layer3/cat'+str(cat)+'.npz'
ff=np.load(fname)
img_vc=ff['vc_score']
vc_num=len(img_vc[0])
img_num=len(img_vc)
img_vc_avg=[]
for i in range(vc_num):
    img_vc_avg.append(float(np.sum(img_vc[np.where(img_vc[:,i]!=-1),i]))/img_num)
img_vc_avg=np.asarray(img_vc_avg)
rindexsort=np.argsort(-img_vc_avg)

big_img = np.zeros((5+ss, 5+(ss+5)*len(pool4_merge), 3))


for j in range(len(pool4_merge)):

	rnum=5
	cnum=5+j*(ss+5)
	select_vc=rindexsort[pool4_merge[j]]
	big_img[rnum:rnum+ss, cnum:cnum+ss, :] = example[select_vc][:,0].reshape(ss,ss,3).astype(int)

fname = '/data2/xuyangf/OcclusionProject/NaiveVersion/example/tmpexample/'+str(cat)+'.png'
cv2.imwrite(fname, big_img)
コード例 #44
0
ファイル: utils.py プロジェクト: arichadda/yolov5
def plot_images(images, targets, paths=None, fname='images.jpg', names=None, max_size=640, max_subplots=16):
    tl = 3  # line thickness
    tf = max(tl - 1, 1)  # font thickness
    if os.path.isfile(fname):  # do not overwrite
        return None

    if isinstance(images, torch.Tensor):
        images = images.cpu().float().numpy()

    if isinstance(targets, torch.Tensor):
        targets = targets.cpu().numpy()

    # un-normalise
    if np.max(images[0]) <= 1:
        images *= 255

    bs, _, h, w = images.shape  # batch size, _, height, width
    bs = min(bs, max_subplots)  # limit plot images
    ns = np.ceil(bs ** 0.5)  # number of subplots (square)

    # Check if we should resize
    scale_factor = max_size / max(h, w)
    if scale_factor < 1:
        h = math.ceil(scale_factor * h)
        w = math.ceil(scale_factor * w)

    # Empty array for output
    mosaic = np.full((int(ns * h), int(ns * w), 3), 255, dtype=np.uint8)

    # Fix class - colour map
    prop_cycle = plt.rcParams['axes.prop_cycle']
    # https://stackoverflow.com/questions/51350872/python-from-color-name-to-rgb
    hex2rgb = lambda h: tuple(int(h[1 + i:1 + i + 2], 16) for i in (0, 2, 4))
    color_lut = [hex2rgb(h) for h in prop_cycle.by_key()['color']]

    for i, img in enumerate(images):
        if i == max_subplots:  # if last batch has fewer images than we expect
            break

        block_x = int(w * (i // ns))
        block_y = int(h * (i % ns))

        img = img.transpose(1, 2, 0)
        if scale_factor < 1:
            img = cv2.resize(img, (w, h))

        mosaic[block_y:block_y + h, block_x:block_x + w, :] = img
        if len(targets) > 0:
            image_targets = targets[targets[:, 0] == i]
            boxes = xywh2xyxy(image_targets[:, 2:6]).T
            classes = image_targets[:, 1].astype('int')
            gt = image_targets.shape[1] == 6  # ground truth if no conf column
            conf = None if gt else image_targets[:, 6]  # check for confidence presence (gt vs pred)

            boxes[[0, 2]] *= w
            boxes[[0, 2]] += block_x
            boxes[[1, 3]] *= h
            boxes[[1, 3]] += block_y
            for j, box in enumerate(boxes.T):
                cls = int(classes[j])
                color = color_lut[cls % len(color_lut)]
                cls = names[cls] if names else cls
                if gt or conf[j] > 0.3:  # 0.3 conf thresh
                    label = '%s' % cls if gt else '%s %.1f' % (cls, conf[j])
                    plot_one_box(box, mosaic, label=label, color=color, line_thickness=tl)

        # Draw image filename labels
        if paths is not None:
            label = os.path.basename(paths[i])[:40]  # trim to 40 char
            t_size = cv2.getTextSize(label, 0, fontScale=tl / 3, thickness=tf)[0]
            cv2.putText(mosaic, label, (block_x + 5, block_y + t_size[1] + 5), 0, tl / 3, [220, 220, 220], thickness=tf,
                        lineType=cv2.LINE_AA)

        # Image border
        cv2.rectangle(mosaic, (block_x, block_y), (block_x + w, block_y + h), (255, 255, 255), thickness=3)

    if fname is not None:
        mosaic = cv2.resize(mosaic, (int(ns * w * 0.5), int(ns * h * 0.5)), interpolation=cv2.INTER_AREA)
        cv2.imwrite(fname, cv2.cvtColor(mosaic, cv2.COLOR_BGR2RGB))

    return mosaic
コード例 #45
0
ファイル: combine.py プロジェクト: supragyashrestha/Dehazing
import cv2
import itertools
import glob
import natsort

source1 = "./reside_dataset/reside_pix2pix/temp/"
source2 = "./reside_dataset/reside_pix2pix/temp1/"
destination = "./reside_dataset/reside_pix2pix/train/"

list1 = os.listdir(source1)
list2 = os.listdir(source2)
list1 = natsort.natsorted(list1,reverse=True)
list2 = natsort.natsorted(list2,reverse=True)
# for (i,j) in zip(list1,list2):
#     print(i,j)

count = 1399
for (img1,img2) in zip(list1,list2):
    im1 = cv2.imread(source1 + img1)
    im2 = cv2.imread(source2 + img2)
    
    
    width = int(im1.shape[1] * (256/620) )
    height = int(im1.shape[0] * (256/460) )
    dim = (width, height)
    img_resized_1 = cv2.resize(im1, dim, interpolation = cv2.INTER_AREA)
    img_resized_2 = cv2.resize(im2, dim, interpolation = cv2.INTER_AREA)

    img_con_h = cv2.hconcat([img_resized_1, img_resized_2])
    cv2.imwrite(destination + str(count).zfill(6) + ".png", img_con_h )
    count = count+1
コード例 #46
0
ファイル: vis_ekf.py プロジェクト: Ryuta-S/ppanet
def main(argv):
    tf.logging.set_verbosity(tf.logging.INFO)
    common.print_args()



    EKF_VIS_DIR = 'EKF_RESULTS'
    ekf_vis_dir = os.path.join(FLAGS.ckpt_dir, EKF_VIS_DIR)

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

    with tf.Graph().as_default():
        dataset = generator.Dataset(
            dataset_dir=os.path.join('dataset', 'cityscapes', 'tfrecord'),
            dataset_name='cityscapes',
            split_name=['val_fine'],
            batch_size=1,
            crop_size=[HEIGHT, WIDTH],
            is_training=False,
            model_variant='resnet_v1_101_beta',
            min_scale_factor=0.50,
            max_scale_factor=2.0,
            should_shuffle=False,
            should_repeat=False)

        ite = dataset.get_one_shot_iterator()
        sample = ite.get_next()
        images, labels = sample[common.IMAGE], sample[common.LABEL]

        module_order = search_model()

        model = segModel.SegModel(
            num_classes=dataset.num_classes,
            model_variant='resnet_v1_101_beta',
            output_stride=16,
            backbone_atrous_rates=[1, 2, 4],
            is_training=False,
            ppm_rates=[1, 2, 3, 6],
            module_order=module_order,
            decoder_output_stride=4)

        logits = model.build(images=images)
        logits = tf.image.resize_bilinear(logits, tf.shape(images)[1:3], align_corners=True)
        # logits = tf.nn.softmax(logits, axis=3)

        height_ind = tf.range(HEIGHT, dtype=tf.int32)
        width_ind = tf.range(WIDTH, dtype=tf.int32)
        height_ind = tf.expand_dims(tf.math.logical_and(tf.math.greater_equal(height_ind, FLAGS.mask_heights[0]),
                                                        tf.math.greater_equal(FLAGS.mask_heights[1], height_ind)), 1)

        width_ind = tf.expand_dims(tf.math.logical_and(tf.math.greater_equal(width_ind, FLAGS.mask_widths[0]),
                                                        tf.math.greater_equal(FLAGS.mask_widths[1], width_ind)), 0)
        # height_ind = tf.expand_dims(tf.math.equal(height_ind, HEIGHT//4), 1)
        # width_ind = tf.expand_dims(tf.math.equal(width_ind, WIDTH//4), 0)

        height_map = []
        width_map = []
        for w in range(WIDTH):
            height_map.append(height_ind)
        for h in range(HEIGHT):
            width_map.append(width_ind)
        height_map = tf.concat(height_map, axis=1)
        width_map = tf.concat(width_map, axis=0)

        height_map = tf.cast(height_map, tf.float32)
        width_map = tf.cast(width_map, tf.float32)
        mask = tf.expand_dims(tf.math.multiply(height_map, width_map), axis=2)

        m_concat = []
        for _ in range(dataset.num_classes):
            m_concat.append(mask)
        mask = tf.concat(m_concat, axis=2)
        masked_logits = tf.multiply(mask, logits)
        grad = tf.gradients(masked_logits, [images])

        ########### SESSION CREATING PROCESS #############
        checkpoints_iterator = tf.contrib.training.checkpoints_iterator(
            FLAGS.ckpt_dir)

        for checkpoint_path in checkpoints_iterator:
            restorer = tf.train.Saver()
            scaffold = tf.train.Scaffold(init_op=tf.global_variables_initializer(),
                                         saver=restorer,
                                         ready_for_local_init_op=None)

            session_creator = tf.train.ChiefSessionCreator(
                scaffold=scaffold,
                master='',
                checkpoint_filename_with_path=checkpoint_path)

            with tf.train.MonitoredSession(
                    session_creator=session_creator, hooks=None) as sess:
                grad_list = []
                batch_num = 0
                while not sess.should_stop():
                    im, l, g = sess.run([images, logits, grad])
                    grad_list.append(g[0])
                    im = im[0]
                    g = np.abs(g[0][0])

                    if batch_num == 0:
                        sum_g = g
                    else:
                        sum_g += g
                    batch_num += 1

                    g = normalizeImg(g)
                    im = normalizeImg(im)

                    img_path = os.path.join(ekf_vis_dir, '{}_img.jpg'.format(batch_num))
                    grad_path = os.path.join(ekf_vis_dir, '{}_grad.jpg'.format(batch_num))

                    cv2.imwrite(img_path, cv2.cvtColor(im*255, cv2.COLOR_RGB2BGR))
                    cv2.imwrite(grad_path, cv2.cvtColor(g*255, cv2.COLOR_RGB2BGR))

                    print('Processing {} done!'.format(batch_num))
                    if batch_num % 100 == 0:
                        print('100 second sleep')
                        time.sleep(100)
                    if batch_num >= 20:
                        break

            break

        sum_g = sum_g / batch_num
        print('max: {:.3f}, min: {:.3f}'.format(np.max(sum_g), np.min(sum_g)))
        sum_g = normalizeImg(sum_g)
        binary_g = sum_g.copy()
        binary_g[sum_g > np.mean(sum_g)] = 255

        cv2.imwrite(os.path.join(ekf_vis_dir, 'average_grad.jpg'), cv2.cvtColor(sum_g*255, cv2.COLOR_RGB2BGR))

        plt.title('average grad')
        plt.imshow(sum_g)
        plt.show()

        cv2.imwrite(os.path.join(ekf_vis_dir, 'sum_grad_{}.jpg'.format(batch_num)), cv2.cvtColor(binary_g.astype(np.uint8), cv2.COLOR_RGB2BGR))
コード例 #47
0
from keras.optimizers import Adam
from data_loader import load_cucumber
from train import normalize, denormalize


if __name__ == '__main__':
    iterations = 100
    input_dim = 30
    anogan_optim = Adam(lr=0.001, amsgrad=True)

    ### 0. prepare data
    X_train, X_test, y_test = load_cucumber()
    X_train = normalize(X_train)
    X_test = normalize(X_test)
    input_shape = X_train[0].shape

    ### 1. train generator & discriminator
    dcgan = DCGAN(input_dim, input_shape)
    dcgan.load_weights('weights/generator_3999.h5', 'weights/discriminator_3999.h5')

    for i, test_img in enumerate(X_test):
        test_img = test_img[np.newaxis,:,:,:]
        anogan = ANOGAN(input_dim, dcgan.g)
        anogan.compile(anogan_optim)
        anomaly_score, generated_img = anogan.compute_anomaly_score(test_img, iterations)
        generated_img = denormalize(generated_img)
        imgs = np.concatenate((denormalize(test_img[0]), generated_img[0]), axis=1)
        cv2.imwrite('predict' + os.sep + str(int(anomaly_score)) + '_' + str(i) + '.png', imgs)
        print(str(i) + ' %.2f'%anomaly_score)
        with open('scores.txt', 'a') as f:
            f.write(str(anomaly_score) + '\n')
コード例 #48
0
ファイル: hw1.1.py プロジェクト: mjtaheri11/hw1.1
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
font = cv2.FONT_HERSHEY_SIMPLEX
bottomLeftCornerOfText = (10, 100)
fontScale = 1
fontColor = (0, 0, 0)
lineType = 2

cv2.putText(img, '98205849',
            bottomLeftCornerOfText,
            font,
            fontScale,
            fontColor,
            lineType)

cv2.putText(gray, '98205849 ',
            bottomLeftCornerOfText,
            font,
            fontScale,
            fontColor,
            lineType)
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.imshow('image', gray)
k = cv2.waitKey(0)
if k == ord('e'):  # wait for 'e' key to exit
    cv2.destroyAllWindows()
elif k == ord('s'):  # wait for 's' key to save and exit
    cv2.imwrite('edited_img.png', img)
    cv2.imwrite('edited_img1.png', gray)
    cv2.destroyAllWindows()
コード例 #49
0
def take_photos(photos_path):
    '''
    Take photos from a camera. 
    User is required to press SPACEBAR to take photos

    Arguments:
    1. photos_path: the path to store the photos


    Returns:
    1. img_num:     total number of photos taken
    '''
    

    # NOTE: To handle non-consecutive image naming
    # For example, the current image file names might be 
    # ['0001.jpg', '0004.jpg', '0006.jpg, 0007.jpg']
    # 
    # When taking photos, the new naming should first use this set
    # 0000.jpg, 0002.jpg, 0003.jpg, 0005.jpg
    #
    # then, followed by
    # 0008.jpg, 0009.jpg, 0010.jpg ... and so on
    photos_exist = [x for x in os.listdir(photos_path) if not x.startswith('.')]
    num_photos_exist = len(photos_exist)
    idx_required = []
    idx_max = 0


    # Check if there are photos in photos_path
    if num_photos_exist > 0:
        # Convert all photos name to integer value
        idx = [ 0 if photo == "0000.jpg" else int(os.path.splitext(photo)[0].lstrip('0')) for photo in photos_exist ]
        idx_max = max(idx)
        
        # Find the missing integer between 0 to n (inclusive)
        # These indices would be used for saving the image first
        all_idx = list(range(0, idx_max+1))
        idx_required = sorted( list(set(all_idx).difference(set(idx))) )
        idx_max = idx_max + 1





    frame_name = "Photo Taking"
    
    cam = VideoStream(0).start()

    cv2.namedWindow(frame_name)
    cv2.moveWindow(frame_name, 500,200);

    img_num = 0

    while True:
        frame = cam.read()
        if frame.any() == None:
            break

        frame = resize(frame, width=1024)
        frame = cv2.flip(frame, 1)

        frame_with_text = np.copy(frame)
        
        text = "Number of photos taken = " + str(img_num) + "/10"
        cv2.putText(frame_with_text, text, (20, 450),
                cv2.FONT_HERSHEY_TRIPLEX, 1, (255, 255, 0), 2)

        text = "Press SPACEBAR to take photo"
        cv2.putText(frame_with_text, text, (20, 500),
                cv2.FONT_HERSHEY_TRIPLEX, 1, (255, 0, 255), 2)

        text = "Press ESC to Exit"
        cv2.putText(frame_with_text, text, (20, 550),
                cv2.FONT_HERSHEY_TRIPLEX, 1, (0, 128, 0), 2)



        cv2.imshow(frame_name, frame_with_text)

        # TODO: Handle unicode input
        #       Now, if the keyboard is Chinese or Russian
        #       and when the keystroke (QWER ..etc) is pressed
        #       the program stops with assertion error
        # k = cv2.waitKey(1)
        k = cv2.waitKeyEx(1)

        # ESC is pressed or 10 photos are taken
        if k % 256 == 27 or img_num >= 10:
            break

        # SPACEBAR is pressed to take a photo
        elif k % 256 == 32:

            if len(idx_required) > 0:
                name = str(idx_required.pop(0)).zfill(4)
            else:
                name = str(idx_max).zfill(4)
                idx_max += 1
            
            img_name = os.path.join(photos_path, name + ".jpg")
            cv2.imwrite(img_name, frame) 
            img_num += 1
            print("Image saved to " + img_name)



    cam.stop()
    cv2.destroyAllWindows()

    return img_num
コード例 #50
0
from sys import argv
script, filename = argv
import cv2
import numpy as np
import time

img = cv2.imread(filename)

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# surf.hessianThreshold=3000
surf = cv2.SURF(3000)
print "**********88"
kp, res = surf.detectAndCompute(gray, None)
print res.shape
img = cv2.drawKeypoints(gray, kp)
#img = cv2.drawKeypoints(img,kp,None,(255,0,255),4)
print(len(kp))

cv2.namedWindow("SURF")
cv2.imshow("SURF", img)
cv2.imwrite("surf.jpg", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
コード例 #51
0
	# Export the frame within the being/end slice
	video_filename = ann['video_filename']

	video_file = glob.glob(join(VIDEO_DIR, '**/' + video_filename))[0]
	video_output_dir = join(join(OUTPUT_VIDEO_DIR, str(n_objects)), os.path.splitext(video_filename)[0])
	if not os.path.exists(video_output_dir):
		os.makedirs(video_output_dir)

	cap = cv2.VideoCapture(video_file)
	success, frame_image = cap.read()
	frame = 1
	while success:
		frame_file = join(video_output_dir, '%d.png'%(frame - frame_begin))
		success, frame_image = cap.read()
		if frame >= frame_begin and frame < frame_end:
			cv2.imwrite(frame_file, frame_image)
		frame += 1

	print('Generated sliced video for %d/%d'%(ann_idx, len(annotation_files)))



#================================

# Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
# and associated documentation files (the "Software"), to deal in the Software without restriction, 
# including without limitation the rights to use, copy, modify, merge, publish, distribute, 
# sublicense, and/or sell copies of the Software, and to permit persons to whom the Software 
# is furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all copies 
コード例 #52
0
            for c in range(C):
                out[pad + y, pad + x, c] = np.median(tmp[y:y + K_size, x:x + K_size, c])
    out = out[pad:pad + H, pad:pad + W].astype(np.uint8)
    return out
# Average filter

# 作为对比
def average_filter(img, G=3):
    out = img.copy()
    H, W, C = img.shape
    Nh = int(H / G)
    Nw = int(W / G)
    for y in range(Nh):
        for x in range(Nw):
            for c in range(C):
                out[G * y:G * (y + 1), G * x:G * (x + 1), c] = np.mean(
                    out[G * y:G * (y + 1), G * x:G * (x + 1), c]).astype(np.int)
    return out


# Read image

pic_name = input('picture name: ')
img = cv2.imread(pic_name)
# Median Filter and Average Filter
out1 = median_filter(img, K_size=3)
# Save result
cv2.imwrite("../report/media_out1.jpg", out1)
cv2.waitKey(0)
cv2.destroyAllWindows()
コード例 #53
0
    backbone.load_state_dict(torch.load('weights/backbone.pth')['net'])
backbone = backbone.to(device)
backbone.eval()

print('==> Processing data ...')
with torch.no_grad():
    anchor_features = get_features(anchor_loader, len_anchor,
                                   len(anchor_dataset))
    test_features = get_features(test_loader, len_test, len(test_dataset))

    similarity = anchor_features.dot(test_features.transpose())
    result_dir = 'result'
    if not os.path.exists(result_dir):
        os.mkdir(result_dir)
    for i in range(similarity.shape[1]):
        top1 = np.argmax(similarity[:, i])
        name = anchor_dataset.image_labels[top1]
        prob = np.round(
            (1.0 / (1 + np.exp(-9.8 * similarity[top1, i] + 3.763)) *
             1.0023900077282935 + -1.290327970702342e-06),
            decimals=4) * 100
        # if prob < 60:
        #     continue
        prob = f'{prob:.2f}'
        src_img = cv2.imread(test_dataset.image_names[i])
        rst_name = os.path.join(
            result_dir, name + '-' + prob + '-' +
            test_dataset.image_names[i].split('.')[0].split('/')[-1] + '.jpg')
        cv2.imwrite(rst_name, src_img)
        print('Saving {} ...'.format(rst_name))
コード例 #54
0
        # sample point to avoid out of memory
        sample_int = sampleIntensities([x[:, :, i] for x in image_list])

        # recover response curse
        response_curve = gslove(image_list, sample_int, 100, i)

        # reconstruct radiance map

        tmp_i = reconstruct_radiance_map(image_list, response_curve, i)
        tmp_i = cv2.normalize(tmp_i,
                              None,
                              alpha=0,
                              beta=255,
                              norm_type=cv2.NORM_MINMAX)
        radiance_img.append(tmp_i)

    radiance_img = np.array(radiance_img)

    # output : reshape radiance image to rgb image
    output = np.zeros((radiance_img.shape[1], radiance_img.shape[2], 3))
    for i in range(3):
        output[:, :, i] = radiance_img[i]

    # TODO : tonemapping

    # cv2.imwrite(out_dir+'test_out.hdr',output)
    # output = tone_mapping(output, 1.5)
    # output = cv2.normalize(output, None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX)
    cv2.imwrite(out_dir + 'test_out.png', output)
コード例 #55
0
def eval_with_plac(img_dir, det_net, num_imgs, image_ext, draw_imgs=False):

    # 1. preprocess img
    img_plac = tf.placeholder(dtype=tf.uint8, shape=[None, None, 3])  # is RGB. not BGR
    img_batch = tf.cast(img_plac, tf.float32)

    img_batch = short_side_resize_for_inference_data(img_tensor=img_batch,
                                                     target_shortside_len=cfgs.IMG_SHORT_SIDE_LEN,
                                                     length_limitation=cfgs.IMG_MAX_LENGTH)
    if cfgs.NET_NAME in ['resnet152_v1d', 'resnet101_v1d', 'resnet50_v1d']:
        img_batch = (img_batch / 255 - tf.constant(cfgs.PIXEL_MEAN_)) / tf.constant(cfgs.PIXEL_STD)
    else:
        img_batch = img_batch - tf.constant(cfgs.PIXEL_MEAN)

    img_batch = tf.expand_dims(img_batch, axis=0)

    detection_scores, detection_category, detection_boxes_angle = det_net.build_whole_detection_network(
        input_img_batch=img_batch,
        gtboxes_batch_h=None,
        gtboxes_batch_r=None,
        gt_encode_label=None)

    init_op = tf.group(
        tf.global_variables_initializer(),
        tf.local_variables_initializer()
    )

    restorer, restore_ckpt = det_net.get_restorer()

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True

    with tf.Session(config=config) as sess:
        sess.run(init_op)
        if not restorer is None:
            restorer.restore(sess, restore_ckpt)
            print('restore model')

        all_boxes_r = []
        imgs = os.listdir(img_dir)
        pbar = tqdm(imgs)
        for a_img_name in pbar:
            a_img_name = a_img_name.split(image_ext)[0]

            raw_img = cv2.imread(os.path.join(img_dir,
                                              a_img_name + image_ext))
            raw_h, raw_w = raw_img.shape[0], raw_img.shape[1]

            resized_img, det_boxes_r_, det_scores_r_, det_category_r_ = \
                sess.run(
                    [img_batch, detection_boxes_angle, detection_scores, detection_category],
                    feed_dict={img_plac: raw_img[:, :, ::-1]}
                )

            if draw_imgs:
                detected_indices = det_scores_r_ >= cfgs.VIS_SCORE
                detected_scores = det_scores_r_[detected_indices]
                detected_boxes = det_boxes_r_[detected_indices]
                detected_categories = det_category_r_[detected_indices]

                det_detections_r = draw_box_in_img.draw_boxes_with_label_and_scores(np.squeeze(resized_img, 0),
                                                                                    boxes=detected_boxes,
                                                                                    labels=detected_categories,
                                                                                    scores=detected_scores,
                                                                                    method=1,
                                                                                    in_graph=True)

                save_dir = os.path.join('test_hrsc', cfgs.VERSION, 'hrsc2016_img_vis')
                tools.mkdir(save_dir)

                cv2.imwrite(save_dir + '/{}.jpg'.format(a_img_name),
                            det_detections_r[:, :, ::-1])

            if det_boxes_r_.shape[0] != 0:
                resized_h, resized_w = resized_img.shape[1], resized_img.shape[2]
                det_boxes_r_ = forward_convert(det_boxes_r_, False)
                det_boxes_r_[:, 0::2] *= (raw_w / resized_w)
                det_boxes_r_[:, 1::2] *= (raw_h / resized_h)
                det_boxes_r_ = backward_convert(det_boxes_r_, False)

            x_c, y_c, w, h, theta = det_boxes_r_[:, 0], det_boxes_r_[:, 1], det_boxes_r_[:, 2], \
                                    det_boxes_r_[:, 3], det_boxes_r_[:, 4]

            boxes_r = np.transpose(np.stack([x_c, y_c, w, h, theta]))
            dets_r = np.hstack((det_category_r_.reshape(-1, 1),
                                det_scores_r_.reshape(-1, 1),
                                boxes_r))
            all_boxes_r.append(dets_r)

            pbar.set_description("Eval image %s" % a_img_name)

        # fw1 = open(cfgs.VERSION + '_detections_r.pkl', 'wb')
        # pickle.dump(all_boxes_r, fw1)
        return all_boxes_r
コード例 #56
0
loaddir='sketch_png'
savedir='sketch_cls'
outsize=128
numclusters=5
finaldict={}
with open('categories.txt') as file:
	for line in file:
		ipdir=os.path.join(loaddir,line.strip())
		classfile=os.listdir(ipdir)
		classdata=np.zeros((len(classfile),outsize**2))
		for cfile in range(len(classfile)):
			ipfilename=os.path.join(ipdir,classfile[cfile])
			tempimage=cv2.imread(ipfilename,0)
			tempimage=cv2.resize(tempimage,(outsize,outsize))
			classdata[cfile,:]=tempimage.ravel()
			classfile[cfile]=classfile[cfile].split('.')[0]
		classdata=classdata/255.
		kmeansout=KMeans(n_clusters=numclusters,random_state=0).fit(classdata)
		kmeanscls=list(kmeansout.labels_)
		print str(Counter(kmeanscls))
		finaldict[line.strip()]=dict(zip(classfile,kmeanscls))
		opdir=os.path.join(savedir,line.strip())
		if not os.path.exists(opdir):
			os.mkdir(opdir)
		outputfiles=kmeansout.cluster_centers_.reshape((numclusters,outsize,outsize))
		for i in range(numclusters):
			opfilename=os.path.join(opdir,line.strip()+'%'+str(i).zfill(2)+'.png')
			cv2.imwrite(opfilename,(outputfiles[i,:,:]*255).astype(int))
		print line.strip()+' Done......'

pickle.dump(finaldict,open('clusters.p','wb'))
コード例 #57
0
import numpy as np
import cv2
import glob
import os
import sys

for filename in glob.glob("train/Seq01/imagesjpg/*.jpg"):#specify your own path
	img = cv2.imread(filename)
	img = cv2.cvtColor( img, cv2.COLOR_RGB2GRAY )
	cv2.imwrite( filename, img )


コード例 #58
0
        font = cv2.FONT_HERSHEY_SIMPLEX
        nm = nm.replace("V ",
                        "").replace("Liquid Suspension",
                                    "Suspension").replace(" GENERAL", "")
        ImOverlay1 = Im.copy()
        ImOverlay1[:, :, 1][Lb == 1] = 0
        cv2.putText(ImOverlay1, nm, (int(w / 3), int(h / 6)), font, 2,
                    (0, 255, 0), 2, cv2.LINE_AA)
        ImOverlay1[:, :, 0][Lb == 1] = 255
        OutMain[h * y:h * (y + 1), w * x:w * (x + 1)] = ImOverlay1
        x += 1
        if x >= mx:
            x = 0
            y += 1
    h, w, d = OutMain.shape
    cv2.imwrite(OutDir + "/" + name[:-4] + "_Main.png", OutMain)
    fr = np.max([h / 700, w / 1400])
    OIm = np.zeros([704, 1404, 3], np.uint8)
    OutMain = cv2.resize(OutMain, (int(w / fr), int(h / fr)))
    h, w, d = OutMain.shape
    OIm[0:h, 0:w, :] = OutMain
    print(OIm.shape)
    MainCatsVideoWriter.write(OIm)
    cv2.imwrite(OutDir + "/" + name[:-4] + "_Main.png", OIm)

    cv2.imshow('Main Classes', OIm)

    cv2.waitKey(25)

    #------------------------------------Display all classes on the image----------------------------------------------------------------------------------
    h, w, d = Im.shape
コード例 #59
0
            cat_name = one['cat_name']
            labels.append("%s: %.2f"%(cat_name, float(one['score'])))
            if not color_assign.has_key(cat_name):
              this_color = color_queue.pop()
              color_assign[cat_name] = this_color
              # recycle it
              color_queue.insert(0, this_color)
            color = color_assign[cat_name]
            box_colors.append(color)

      else:
        if args.show_only_result_frame:
          continue

      ori_im = cv2.imread(frame, cv2.IMREAD_COLOR)

      new_im = draw_boxes(ori_im, boxes, labels, box_colors, font_scale=0.8,
                          font_thick=10, box_thick=2, bottom_text=False)

      if args.show_frame_num:
        # write the frame idx
        cv2.putText(new_im, "# %d" % frameIdx,
                    (0, 20), cv2.FONT_HERSHEY_SIMPLEX,
                    1, (0, 255, 0), 2)
      if args.show_only_result_frame:
        filename = "%08d" % actual_count
        actual_count += 1
      target_file = os.path.join(target_path, "%s.jpg" % filename)

      cv2.imwrite(target_file, new_im)
コード例 #60
0
                                               "default: piece_parameters.json",
                    default="piece_parameters.json")
    ap.add_argument('-s', '--save', help="Path to the created pattern", required=True)
    args = vars(ap.parse_args())

    border, shape_height, rect_width, start_x, start_y, cyan, \
        yellow, magenta, t_height, t_width = pattern_pars('piece_parameters.json')

    c = 0
    # the shape is symmetrical and the pattern depends on the initial parameters above
    # when the first side is drawn the shape is rotated at 90 degrees and the next side is drawn in the same fashion
    img = np.zeros((t_height, t_width, 3), np.uint8)
    img[:] = (0, 0, 0)
    while c < 4:
        img = side_drawer(shape_height, rect_width, start_x, start_y, cyan, yellow, magenta)
        cols, rows, dims = img.shape
        M = cv2.getRotationMatrix2D((cols / 2, rows / 2), 90, 1)
        img = cv2.warpAffine(img, M, (cols, rows))
        c += 1

    # Draw a white frame
    cv2.rectangle(img, (0, 0), (t_height, t_width), (255, 255, 255), 120)
    # Draw the ROI area
    cv2.rectangle(img, (cyan, cyan), (380, 380), (255, 255, 255), -1)

    # Save
    name = os.path.join(args["save"], "piece.jpg")
    cv2.imwrite(name, img)
    # Display the image
    cv2.imshow("img", img)
    cv2.waitKey(0)