def GauPyr(self,img): ''' 入力された画像のgaussian pyramidを計算する args : img -> Mat,Uint8,1chの画像 dst : pyr -> 元画像合わせて10階層のgaussian画像が入ったlist,それぞれargsと同じサイズ pyr[i].shapeは,src.shape / 2**i pyr2 -> 極端に差がでるpyr ''' pyr = range(10) pyr2 = range(10) pyr[0] = img pyr2[0] = img # Create Gaussian pyramid for i in range(1,10): pyr[i] = cv2.pyrDown(pyr[i-1]) pyr2[i] = cv2.GaussianBlur(pyr[i-1], (9,9), 2**1) # Resize pyramid for i in range(10): pyr[i] = cv2.resize(pyr[i],(512,512), interpolation = cv2.INTER_LINEAR) pyr2[i] = cv2.resize(pyr2[i],(512,512), interpolation = cv2.INTER_LINEAR) # print np.array(pyr[i]).shape # cv2.imshow('r%s'%i,pyr2[i]/np.amax(pyr[i])) return pyr
def dewarp(imagedir): # Loading from json file C = CameraParams.fromfile(os.path.join(imagedir, "params.json")) K = C.K D = C.D print("Loaded camera parameters from " + os.path.join(imagedir, "params.json")) for f in file_list(imagedir, ['jpg', 'jpeg', 'png']): print(f) colour = cv2.imread(f) grey = cv2.cvtColor(colour, cv2.COLOR_BGR2GRAY) h, w = grey.shape[:2] newcameramtx, roi=cv2.getOptimalNewCameraMatrix(K, D, (w,h), 1, (w,h)) mapx, mapy = cv2.initUndistortRectifyMap(K, D, None, newcameramtx, (w,h), 5) dewarped = cv2.remap(grey, mapx, mapy, cv2.INTER_LINEAR) x, y, w, h = roi dewarped = dewarped[y:y+h, x:x+w] grey = cv2.resize(grey, (0,0), fx=0.5, fy=0.5) dewarped = cv2.resize(dewarped, (0,0), fx=0.5, fy=0.5) cv2.imshow("Original", grey ) cv2.imshow("Dewarped", dewarped) cv2.waitKey(-1)
def _upSample(I, scale=2, shape=None): if shape is not None: h, w = shape return cv2.resize(I, (w, h), interpolation=cv2.INTER_LINEAR) h, w = I.shape[:2] return cv2.resize(I, (w * scale, h * scale), interpolation=cv2.INTER_LINEAR)
def crop(img, segimg, fx, fy, cx, cy): # Perform center cropping, preserving 50% vertically. middle_perc = 0.50 left = 1-middle_perc half = left/2 a = img[int(img.shape[0]*(half)):int(img.shape[0]*(1-half)), :] aseg = segimg[int(segimg.shape[0]*(half)):int(segimg.shape[0]*(1-half)), :] cy /= (1/middle_perc) # Resize to match target height while preserving aspect ratio. wdt = int((128*a.shape[1]/a.shape[0])) x_scaling = float(wdt)/a.shape[1] y_scaling = 128.0/a.shape[0] b = cv2.resize(a, (wdt, 128)) bseg = cv2.resize(aseg, (wdt, 128)) # Adjust intrinsics. fx*=x_scaling fy*=y_scaling cx*=x_scaling cy*=y_scaling # Perform center cropping horizontally. remain = b.shape[1] - 416 cx /= (b.shape[1]/416) c = b[:, int(remain/2):b.shape[1]-int(remain/2)] cseg = bseg[:, int(remain/2):b.shape[1]-int(remain/2)] return c, cseg, fx, fy, cx, cy
def Visualize(self,img_path, output_path): original_img = cv2.imread(img_path, 1) width, height, _ = original_img.shape #Reshape to the network input shape (3, w, h). #img = np.array([np.transpose(np.float32(original_img), (2, 0, 1))]) #Get the 512 input weights to the softmax. class_weights = self.model.layers[-1].get_weights()[0] final_conv_layer = self.get_output_layer(self.model, "conv2d_26") get_output = K.function([self.model.layers[0].input], \ [final_conv_layer.output, self.model.layers[-1].output]) [conv_outputs, predictions] = get_output([np.array([original_img])]) conv_outputs = conv_outputs[0, :, :, :] print(predictions) #Create the class activation map. cam = np.ones(conv_outputs.shape[0 : 2], dtype = np.float32) target_class = 1 for i, w in enumerate(class_weights[:, target_class]): cam+= w * conv_outputs[:, :,i] print("predictions", predictions) cam /= np.max(cam) cam = cv2.resize(cam, (height, width)) print(cam.shape) cam /= np.max(cam) cam = cv2.resize(cam, (height, width)) heatmap = cv2.applyColorMap(np.uint8(255*cam), cv2.CV_8UC1) heatmap[np.where(cam < 0.2)] = 0 img = heatmap*0.5 + original_img cv2.imwrite(output_path, img)
def normalize_cv(image1,image2,compare_dir): #img = Image.open(image1) #height < width width_heights = [] width_heights.append(["long_dir/",(100,150)]) #width < height width_heights.append(["wide_dir/",(150,100)]) #equal width_heights.append(["equal_dir/",(150,150)]) #= img.size #resize comparison image for width_height in width_heights: if not os.path.exists(width_height[0]): os.mkdir(width_height[0]) os.chdir(width_height[0]) img = cv2.imread(image1) resized_image1 = cv2.resize(img, width_height[1]) new_path1 = path_append(image1,width_height[0]) cv2.imwrite(new_path1,resized_image1) img2 = cv2.imread(image2) resized_image2 = cv2.resize(img2,width_height[1]) new_path2 = path_append(image2,width_height[0]) cv2.imwrite(new_path2,resized_image2) os.chdir("../") #resize test directory for pic in glob(compare_dir+"*"): if os.path.isfile(pic): full_pic = os.path.abspath(pic) im = cv2.imread(full_pic) resized_image = cv2.resize(im, width_height[1]) new_path = path_append(pic,width_height[0]) cv2.imwrite(new_path,resized_image)
def _downSample(I, scale=4, shape=None): if shape is not None: h, w = shape return cv2.resize(I, (w, h), interpolation=cv2.INTER_NEAREST) h, w = I.shape[:2] return cv2.resize(I, (w / scale, h / scale), interpolation=cv2.INTER_NEAREST)
def pro_progess(filepath="../data"): height = 299 train_files = os.listdir(filepath + '/train') train = np.zeros((len(train_files), height, height, 3), dtype=np.uint8) labels = list(filter(lambda x: x[:3] == 'dog', train_files)) test_files = os.listdir(filepath + '/test') test = np.zeros((len(test_files), height, height, 3), dtype=np.uint8) for i in tqdm(range(len(train_files))): filename = filepath + train_files[i] img = cv2.imread(filename) img = cv2.resize(img, (height, height)) train[i] = img[:, :, ::-1] for i in tqdm(range(len(test_files))): filename = filepath + test_files[i] img = cv2.imread(filename) img = cv2.resize(img, (height, height)) test[i] = img[:, :, ::-1] print ('Training Data Size = %.2 GB' % (sys.getsizeof(train)/1024**3)) print ('Testing Data Size = %.2 GB' % (sys.getsizeof(test)/1024**3)) X_train, X_val, y_train, y_val = train_test_split( train, labels, shuffle=True, test_size=0.2, random_state=42) return X_train, X_val, y_train, y_val
def extract(self, image, segments): fs = self.feature_size bg = 255 regions = numpy.ndarray(shape=(0, fs), dtype=FEATURE_DATATYPE) for segment in segments: region = region_from_segment(image, segment) if self.stretch: region = cv2.resize(region, (fs, fs)) else: x, y, w, h = segment proportion = float(min(h, w)) / max(w, h) new_size = (fs, int(fs * proportion)) if min(w, h) == h else (int(fs * proportion), fs) region = cv2.resize(region, new_size) s = region.shape new_region = numpy.ndarray((fs, fs), dtype=region.dtype) new_region[:, :] = bg new_region[:s[0], :s[1]] = region region = new_region regions = numpy.append(regions, region, axis=0) regions.shape = (len(segments), fs**2) return regions
def show_video(name): cap = cv2.VideoCapture(name) rotate = False M = cv2.getRotationMatrix2D((480,270), 180, 1.0) if not cap.isOpened(): print("Error when reading video") else: while(True): try: # Capture frame-by-frame ret, frame = cap.read() frame = cv2.resize(frame, (960,540)) if rotate: frame = cv2.warpAffine(frame, M, (960, 540)) cv2.putText(frame,'press the escape key when done',(20,20), cv2.FONT_HERSHEY_SIMPLEX, 1,(130,130,130),2) cv2.imshow(name,frame) except: cap = cv2.VideoCapture(name) ret, frame = cap.read() frame = cv2.resize(frame, (960,540)) cv2.imshow(name,frame) k = cv2.waitKey(20) if k == 27: break elif k == 114: rotate = False if rotate is True else True # When everything is done, release the capture cap.release() cv2.destroyAllWindows() return rotate
def run(self): while True: ret, frame = self.cam.read() # resize the frame to half the original size img = cv2.resize(frame, (frame.shape[1]/2, frame.shape[0]/2), interpolation = cv2.INTER_CUBIC) imgout = img.copy() for i,r in enumerate(self.detector.detect(img)): x0,y0,x1,y1 = r # get face, convert to grayscale & resize to face_sz face = img[y0:y1, x0:x1] face = cv2.cvtColor(face,cv2.COLOR_BGR2GRAY) face = cv2.resize(face, self.face_sz, interpolation = cv2.INTER_CUBIC) # get a prediction prediction = self.predictor.predict(face) # draw the face area cv2.rectangle(imgout, (x0,y0),(x1,y1),(0,255,0),2) # draw the predicted name (folder name...) draw_str(imgout, (x0-20,y0-20), self.dataSet.names[prediction]) cv2.imshow('videofacerec', imgout) # get pressed key ch = cv2.waitKey(10) if ch == 27: break
def execute(self, image): gray = cv2.cvtColor(image, cv.CV_BGR2GRAY) cv2.equalizeHist(gray, gray) faces = self.face_cascade.detectMultiScale(gray, 1.1, 2, 0 | cv.CV_HAAR_SCALE_IMAGE, (30, 30)) for i in xrange(0, len(faces)): lastface = faces[i - 1] face = faces[i] lastrect = self.get_image_size(image, face) face = cv2.resize(face, (lastmaxy - lastminy, lastmaxx - lastminx)) faceimg, coord = self.get_image_data(image, face) y, x, _ = faceimg.shape if x < smallestx: smallestx = x if y < smallesty: smallesty = y facedata.append((faceimg, coord)) for i in xrange(0, len(facedata)): _, lastcoord = facedata[i - 1] lastminy, lastmaxy, lastminx, lastmaxx = lastcoord face, coord = facedata[i] miny, maxy, minx, maxx = coord face = cv2.resize(face, (lastmaxy - lastminy, lastmaxx - lastminx)) welp = image[lastminy:lastmaxy, lastminx:lastmaxx] image[lastminy:lastmaxy, lastminx:lastmaxx] = cv2.addWeighted( welp, 0.5, face, 0.5, 0.0 ) # cv2.merge((gray,gray,gray)) return image
def setAndShowImage(sender): brightness = sliders[6].get()/10.0 saturation = sliders[7].get()/10.0 # so the range will be between -30 to 30 in jumps of 0.1 cam.set(cv2.cv.CV_CAP_PROP_BRIGHTNESS, brightness) cam.set(cv2.cv.CV_CAP_PROP_SATURATION, saturation) if not isImageChoosed: didGet, frame = cam.read() else: frame = userImage upperH = sliders[0].get() upperS = sliders[1].get() upperV = sliders[2].get() lowerH = sliders[3].get() lowerS = sliders[4].get() lowerV = sliders[5].get() hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) # Threshold the HSV image to get only blue colors. mask = cv2.inRange(hsv, np.array([lowerH,lowerS,lowerV]), np.array([upperH,upperS,upperV])) # Bitwise-AND mask and original image. res = cv2.bitwise_and(frame, frame, mask=mask) maskedImage = res thresh = cv2.threshold(mask, 25, 255, cv2.THRESH_BINARY)[1] # dilate the threshed image to fill in holes, then find contours on thresholded image. thresh = cv2.dilate(thresh, None, iterations=1) threshImage = thresh cv2.imshow("Thresh Image", cv2.resize(threshImage,(480, 320), interpolation = cv2.INTER_CUBIC)) cv2.imshow("Original Image", cv2.resize(frame,(480, 320), interpolation = cv2.INTER_CUBIC)) cv2.imshow("Masked Image", cv2.resize(maskedImage,(480, 320), interpolation = cv2.INTER_CUBIC))
def run(self): while True: ret, frame = self.cam.read() # resize the frame to half the original size img = cv2.resize( frame, (frame.shape[1] / 2, frame.shape[0] / 2), interpolation=cv2.INTER_CUBIC) imgout = img.copy() faces = [] for i, r in enumerate(self.detector.detect(img)): x0, y0, x1, y1 = r # get face, convert to grayscale & resize to face_sz face = img[y0:y1, x0:x1].copy() face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY) face = cv2.resize( face, self.face_sz, interpolation=cv2.INTER_CUBIC) # draw a rectangle to show the detection cv2.rectangle(imgout, (x0, y0), (x1, y1), (0, 255, 0), 1) # and append to currently detected faces faces.append(face) cv2.imshow("detections", imgout) # wait for a key press ch = cv2.waitKey(10) # store the currently detected faces if (ch == ord('s')) and (len(faces) > 0): for face in faces: self.saveImage(face) if ch == 27 or ch == ord('q'): break
def LumConDrift(bgImg,fundusMask): m,n = bgImg.shape tsize = 50 indx=0 indy=0 i = tsize ldrift = np.zeros((int(m/tsize),int(n/tsize)),np.float) cdrift = np.zeros((int(m/tsize),int(n/tsize)),np.float) while(i<m): j = tsize while(j<n): if (i+tsize>=m and j+tsize<n): block = bgImg[i-tsize:m, j-tsize:j+tsize] elif (i+tsize<m and j+tsize>=n): block = bgImg[i-tsize:i+tsize, j-tsize:n] elif (i+tsize>=m and j+tsize>=n): block = bgImg[i-tsize:m, j-tsize:n] else : block = bgImg[i-tsize:i+tsize, j-tsize:j+tsize] mean,std = cv2.meanStdDev(block) ldrift[indx,indy] = mean cdrift[indx,indy] = std indy = indy+1 j = j+tsize indy = 0 indx = indx+1 i = i+tsize ldrift = cv2.resize(ldrift,(n,m),interpolation = cv2.INTER_CUBIC) cdrift = cv2.resize(cdrift,(n,m),interpolation = cv2.INTER_CUBIC) ldrift = cv2.multiply(ldrift,fundusMask.astype(float)) cdrift = cv2.multiply(cdrift,fundusMask.astype(float)) return ldrift,cdrift
def val_data_generator(val_idx, batch_size, validation_steps): while True: inputs = [] outputs = [] step_id = 0 for i in val_idx: img0 = skimage.io.imread(all_files[i], plugin='tifffile') img0 = cv2.resize(img0, (520, 520)) pan = skimage.io.imread(all_pan_files[i], plugin='tifffile') pan = cv2.resize(pan, (520, 520)) pan = pan[..., np.newaxis] img0 = np.concatenate([img0, pan], axis=2) msk = cv2.imread(all_masks[i], cv2.IMREAD_UNCHANGED)[..., 0:1] msk = (msk > 127) * 1 for x0, y0 in [(0, 0)]: img = img0[y0:y0+input_shape[0], x0:x0+input_shape[1], :] otp = msk[y0:y0+input_shape[0], x0:x0+input_shape[1], :] inputs.append(img) outputs.append(otp) if len(inputs) == batch_size: step_id += 1 inputs = np.asarray(inputs) outputs = np.asarray(outputs, dtype='float') inputs = preprocess_inputs_std(inputs, city_id) yield inputs, outputs inputs = [] outputs = [] if step_id == validation_steps: break
def make_effect(u_orig, scale): res_height, res_width = scale * u_orig.shape[0], scale * u_orig.shape[1] # Compute the lightning effect effect = np.fft.irfft2(np.fft.rfft2(2.*(u_orig-0.5))* fft_mask_light) effect /= 30. # HAND TUNED SCALING of the effect ... might need to be adapted if changing s_kernel effect[effect >= 1.0] = 1.0 effect[effect <= 0.0] = 0.0 effect_hires = cv2.resize(effect, (res_width, res_height), interpolation=cv2.INTER_CUBIC) u_hires = cv2.resize(u_orig, (res_width, res_height),interpolation=cv2.INTER_CUBIC) u_hires[u_hires >= 0.5] = 1. u_hires[u_hires < 0.5 ] = 0. # Blur the image to get the shading u_blur = scipy.ndimage.filters.uniform_filter(u_hires, size=5) # Shift the shadding down right u_blur = np.lib.pad(u_blur, ((2,0),(2,0)), 'constant', constant_values=1)[:-2,:-2] dst = 0.6 * u_hires + 0.4 * effect_hires dst[u_hires >= 0.99] = u_blur[u_hires >= 0.99] dst[dst > 1] = 1 dst[dst < 0] = 0 return dst
def process_image(self, inImg): frame = cv2.flip(inImg,1,0) grayImg = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) cropped = cv2.resize(grayImg, (grayImg.shape[1] / self.size, grayImg.shape[0] / self.size)) faces = self.haar_cascade.detectMultiScale(cropped) persons = [] for i in range(len(faces)): face_i = faces[i] x = face_i[0] * self.size y = face_i[1] * self.size w = face_i[2] * self.size h = face_i[3] * self.size face = grayImg[y:y + h, x:x + w] face_resize = cv2.resize(face, (self.im_width, self.im_height)) confidence = self.model.predict(face_resize) # cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 3) if confidence[1]<3100: person = self.names[confidence[0]] cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 3) cv2.putText(frame, '%s - %.0f' % (person, confidence[1]), (x-10, y-10), cv2.FONT_HERSHEY_PLAIN,2,(0, 255, 0),2) else: person = 'Unknown' cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 3) cv2.putText(frame, person, (x-10, y-10), cv2.FONT_HERSHEY_PLAIN,2,(0, 102, 255),2) persons.append(person) return (frame, persons)
def mask_skeleton(img, prev, background): img = cv2.resize(img, (img.shape[1] / IMAGE_SCALE, img.shape[0]/ IMAGE_SCALE)) background = cv2.resize(background, (img.shape[1],img.shape[0])) gray_bg = cv2.cvtColor(background, cv2.COLOR_BGR2GRAY) gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) gray_bg = cv2.GaussianBlur(gray_bg, GAUS_MASK, 0) gray_img = cv2.GaussianBlur(gray_img, GAUS_MASK, 0) frameDelta = cv2.absdiff(gray_bg, gray_img) thresh = cv2.threshold(frameDelta, BG_THRESH, 255, cv2.THRESH_BINARY)[1] # now using the motion # uncomment this for motion usage """ if prev is None: return (thresh, thresh) frameDelta = cv2.absdiff(prev, thresh) motion_thresh = cv2.threshold(frameDelta, THRESH, 255, cv2.THRESH_BINARY)[1] for _ in xrange(1): motion_thresh = cv2.dilate(motion_thresh, None, iterations=20) motion_thresh = cv2.erode(motion_thresh,None,iterations =15) # remove_big_blobs(motion_thresh) # contours # (contours, _) = cv2.findContours(motion_thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # cv2.drawContours(motion_thresh, contours, -1, (0,255,0), 3) return thresh, motion_thresh """ return (thresh, thresh)
def readFrame(self): """read frame from openCV info""" self.previousFrame = self.frameImage success, self.frameImage = self.vidcap.read() self.frameImage = cv2.flip(self.frameImage,1) cv2.resize(self.frameImage, (600, 380), 0, 0, cv2.INTER_CUBIC); return success, self.frameImage
def getIcon(self, maxWidth, maxHeight): # Create an icon of a cropped image of the 1st View, and resize it to the parameters. # if drawPickupRect is True, it will also overlay the position of the robots pickup area for the object # Get the full image and crop it fullImage = self.views[0].image.copy() rect = self.views[0].rect image = fullImage[rect[1]:rect[3], rect[0]:rect[2]] # Draw the pickupArea on it before resizing # if drawPickupRect and self.views[0].pickupRect is not None: x0, y0, x1, y1 = self.views[0].pickupRect quad = np.int32([[x0, y0], [x1, y0], [x1, y1], [x0, y1]]) cv2.polylines(image, [quad], True, (255, 255, 255), 2) # Resize it to fit within maxWidth and maxHeight # Keep Width within maxWidth height, width, _ = image.shape if width > maxWidth: image = cv2.resize(image, (maxWidth, int(float(maxWidth) / width * height))) # Keep Height within maxHeight height, width, _ = image.shape if height > maxHeight: image = cv2.resize(image, (int(float(maxHeight) / height * width), maxHeight)) return image.copy()
def cropandResize_MaskandImage(mask, image): """Crops and resizes both (mask & image) based on mask values to 100x300 pixels. Input: mask - uint8 2D single channel binary[0,1] image (mask) image - uint8 2D single channel gray-scale image Output: imcrop - silhouette cropped image k - flag that indicates when something is detected in the mask >> (2D_numpyarray, 2D_numpyarray) -> (1D_numpyarray, 1D_numpyarray) """ # ensure that the mask is binary in the [0,1] range mask = mask/np.max(mask) rows,cols = np.nonzero(mask==1) # basic check for when there is nothing to mask if ((not(rows.size and cols.size)) or ( (rows.min() == rows.max()) and (cols.min() == cols.max()))): msk_crop = mask img_crop = image k = False print "WARNING: Nothing to crop - returning black (empty) image" else: msk_crop = mask[rows.min():rows.max(), cols.min():cols.max()] img_crop = image[rows.min():rows.max(), cols.min():cols.max()] k = True imcrop = cv2.resize(img_crop, (96,300)) # (100,300)) # paper uses these dimensions but nicer for 6x6 block processes maskcrop = cv2.resize(msk_crop, (96,300)) return imcrop, maskcrop, k
def main(): x = 0 cap1 = cv2.VideoCapture(0) # Camera 1 cap2 = cv2.VideoCapture(1) # Camera 2 @click.command() @click.option('--imwidth', default=1, prompt='Image width') @click.option('--imheight', default=1, prompt='Image height') @click.option('imgs_directory', prompt="Directory to save images in") @click.option('extension', prompt='Image extension') while(1): # Capturing frame by frame ret, img1 = cap1.read() # Capture frame by frame from Camera 1 ret, img2 = cap2.read() # Capture frame by frame from Camera 2 r1 = imwidth / img1.shape[1] # Dividing by the width ratio1 = (imwidth, int(img1.shape[0] * r1)) # multiplying by the height/width r2 = imwidth / img2.shape[1] ratio2 = (imwidth, int(img2.shape[1] * r2)) img_res1 = cv2.resize(img1, ratio1, interpolation=cv2.INTER_AREA) # INTER_AREA optimum for scaling down img_res2 = cv2.resize(img2, ratio2, interpolation=cv2.INTER_AREA) cv2.imshow("IMG1", img_res1) cv2.imshow("IMG2", img_res2) if (cv2.waitKey(30) > 0): x += 1 print "Saving img pair " + x filename1 = "%sleft%d.%s" % (imgs_directory, x, extension) filename2 = "%sright%d.%s" % (imgs_directory, x, extension) cv2.imwrite(filename1, im_res1) cv2.imwrite(filename2, im_res2)
def each_emotion(i): li = lists[i] out = outdir%emotions[i][1] util.make_folder(out) j = 0 while len(os.listdir(out)) < 100000: j += 1 try: filename = li[j].split('/')[-1] filename = filename.split('.')[0] if os.path.isfile('%s/%s_orig.jpg'%(out,filename)) and os.path.isfile('%s/%s_flip.jpg'%(out,filename)) and os.path.isfile('%s/%s_crop1.jpg'%(out,filename)) and os.path.isfile('%s/%s_crop2.jpg'%(out,filename)): continue im = cv.imread(li[j]) flip = cv.flip(im,1) h = im.shape[0] w = im.shape[1] size = min(h,w) cv.imwrite('%s/%s_orig.jpg'%(out,filename), cv.resize(im,(64,64))) cv.imwrite('%s/%s_flip.jpg'%(out,filename), cv.resize(flip,(64,64))) cv.imwrite('%s/%s_crop1.jpg'%(out,filename), cv.resize(im[0:size,0:size],(64,64))) cv.imwrite('%s/%s_crop2.jpg'%(out,filename), cv.resize(im[h-size:h,w-size:w],(64,64))) except: print '%s:%d (%s)'%(emotions[i],j, filename) pass
def read_MSR_depth_ims(depth_file, resize='VGA'): ''' Extracts depth images and masks from the MSR Daily Activites dataset ---Parameters--- depth_file : filename for set of depth images (.bin file) ''' file_ = open(depth_file, 'rb') ''' Get header info ''' frames = np.fromstring(file_.read(4), dtype=np.int32)[0] cols = np.fromstring(file_.read(4), dtype=np.int32)[0] rows = np.fromstring(file_.read(4), dtype=np.int32)[0] ''' Get depth/mask image data ''' data = file_.read() ''' Depth images and mask images are stored together per row. Thus we need to extract each row of size n_cols+n_rows ''' dt = np.dtype([('depth', np.int32, cols), ('mask', np.uint8, cols)]) ''' raw -> usable images ''' frame_data = np.fromstring(data, dtype=dt) depthIms = frame_data['depth'].astype(np.uint16).reshape([frames, rows, cols]) maskIms = frame_data['mask'].astype(np.uint16).reshape([frames, rows, cols]) if resize == 'VGA': # embed() depthIms = np.dstack([cv2.resize(depthIms[d,:,:], (640,480)) for d in xrange(len(depthIms))]) maskIms = np.dstack([cv2.resize(maskIms[d,:,:], (640,480)) for d in xrange(len(maskIms))]) return depthIms, maskIms
def detect_skin( _img, _mask ): ''' Calculate the skin region of _img according to the mean and std of YCrCb channels. _mask: initial mask Pixel which are in [u-2*sigma, u+2*sigma] are considered in the skin region ''' scale = 2.0 img = cv2.resize(_img, dsize = (0,0), fx=1.0/scale, fy=1.0/scale, interpolation=cv2.INTER_NEAREST) mask = cv2.resize(_mask, dsize = (0,0), fx=1.0/scale, fy=1.0/scale, interpolation=cv2.INTER_NEAREST) skinMask = np.zeros( img.shape[:2], np.uint8 ) YCrCb = cv2.cvtColor(img, cv2.COLOR_BGR2YCrCb) Y, Cr, Cb = YCrCb[:,:,0], YCrCb[:,:,1], YCrCb[:,:,2] Uy, Sy = calMeanStd(Y[mask == 1], 0.05, 0.95) Ucb, Scb = calMeanStd(Cb[mask == 1], 0.05, 0.95) Ucr, Scr = calMeanStd(Cr[mask == 1], 0.05, 0.95) for i in range(img.shape[0]): for j in range(img.shape[1]): skinMask[i,j] = 1 if Y[i,j] > Uy - 2*Sy and Y[i,j] < Uy + 2* Sy \ and Cb[i,j] > Ucb - 2* Scb and Cb[i,j] < Ucb + 2* Scb \ and Cr[i,j] > Ucr - 2* Scr and Cr[i,j] < Ucr + 2* Scr else 0 skinMask = cv2.resize(skinMask, dsize = (_img.shape[1], _img.shape[0]), interpolation=cv2.INTER_NEAREST) return skinMask
def updateImage(self): imgCenter = self.winParent.getCameraC().getImage().data if imgCenter is not None: resized = cv2.resize(imgCenter,(self.IMG_WIDTH,self.IMG_HEIGHT)) image = QtGui.QImage(resized.data, resized.shape[1], resized.shape[0], resized.shape[1]*resized.shape[2], QtGui.QImage.Format_RGB888); size=QtCore.QSize(imgCenter.shape[1],imgCenter.shape[0]) self.labelImageCenter.setPixmap(QtGui.QPixmap.fromImage(image)) imgLeft = self.winParent.getCameraL().getImage().data if imgLeft is not None: resized = cv2.resize(imgLeft,(self.IMG_WIDTH,self.IMG_HEIGHT)) image = QtGui.QImage(resized.data, resized.shape[1], resized.shape[0], resized.shape[1]*resized.shape[2], QtGui.QImage.Format_RGB888); size=QtCore.QSize(imgLeft.shape[1],imgLeft.shape[0]) #self.label.resize(size) self.labelImageLeft.setPixmap(QtGui.QPixmap.fromImage(image)) imgRight = self.winParent.getCameraR().getImage().data if imgRight is not None: resized = cv2.resize(imgRight,(self.IMG_WIDTH,self.IMG_HEIGHT)) image = QtGui.QImage(resized.data, resized.shape[1], resized.shape[0], resized.shape[1]*resized.shape[2], QtGui.QImage.Format_RGB888); size=QtCore.QSize(imgRight.shape[1],imgRight.shape[0]) #self.label.resize(size) self.labelImageRight.setPixmap(QtGui.QPixmap.fromImage(image)) #print the filtered images '''
def __getitem__(self, index): """ Args: index (int): Index Returns: tuple: (image, target) where target is index of the target class. """ if self.split == 'train': img_path, target_path = self.train_data[index], self.train_labels[index] elif self.split == 'val': img_path, target_path = self.val_data[index], self.val_labels[index] elif self.split == 'test': img_path, target_path = self.test_data[index], self.test_labels[index] # TODO: Make annotation grayscale so we don't need this hardcoded layer. try: img = cv2.imread(img_path) except: pdb.set_trace() target = cv2.imread(target_path)[:,:,2] img = cv2.resize(img, (self.im_size[1],self.im_size[2])) target = cv2.resize(target, (self.im_size[1], self.im_size[2])) target[target != 0] = 255 # doing this so that it is consistent with all other datasets # to return a PIL Image img = Image.fromarray(img) target = Image.fromarray(target) if self.transform is not None: img = self.transform(img) target = self.transform(target) return img, target
def tower_pos(self, context): img = context["engine"]["frame"][ self.tower_line_top : self.tower_line_top + self.tower_line_height, self.tower_left : self.tower_left + self.tower_width, ] img2 = cv2.resize(img, (self.tower_width, 100)) img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) for i in range(2): img2[20:40, :, i] = cv2.resize(img_hsv[:, :, 0], (self.tower_width, 20)) img2[40:60, :, i] = cv2.resize(img_hsv[:, :, 1], (self.tower_width, 20)) img2[60:80, :, i] = cv2.resize(img_hsv[:, :, 2], (self.tower_width, 20)) # ゲージのうち信頼できる部分だけでマスクする img3 = np.minimum(img, self.ui_tower_mask) # 白い部分にいまヤグラ/ホコがある img3_hsv = cv2.cvtColor(img3, cv2.COLOR_BGR2HSV) white_mask_s = cv2.inRange(img3_hsv[:, :, 1], 0, 8) white_mask_v = cv2.inRange(img3_hsv[:, :, 2], 248, 256) white_mask = np.minimum(white_mask_s, white_mask_v) x_list = np.arange(self.tower_width) tower_x = np.extract(white_mask[3, :] > 128, x_list) tower_xPos = np.average(tower_x) # FixMe: マスクした関係が位置がずれている可能性があるので、適宜補正すべき xPos_pct = (tower_xPos - self.tower_width / 2) / (self.tower_width * 0.86 / 2) * 100 # あきらかにおかしい値が出たらとりあえず排除 if xPos_pct < -120 or 120 < xPos_pct: xPos_pct = Nan return xPos_pct
def rescale_patient_images2(images_zyx, target_shape, verbose=False): if verbose: print("Target: ", target_shape) print("Shape: ", images_zyx.shape) # print "Resizing dim z" resize_x = 1.0 interpolation = cv2.INTER_NEAREST if False else cv2.INTER_LINEAR res = cv2.resize(images_zyx, dsize=(target_shape[1], target_shape[0]), interpolation=interpolation) # opencv assumes y, x, channels umpy array, so y = z pfff # print "Shape is now : ", res.shape res = res.swapaxes(0, 2) res = res.swapaxes(0, 1) # cv2 can handle max 512 channels.. if res.shape[2] > 512: res = res.swapaxes(0, 2) res1 = res[:256] res2 = res[256:] res1 = res1.swapaxes(0, 2) res2 = res2.swapaxes(0, 2) res1 = cv2.resize(res1, dsize=(target_shape[2], target_shape[1]), interpolation=interpolation) res2 = cv2.resize(res2, dsize=(target_shape[2], target_shape[1]), interpolation=interpolation) res1 = res1.swapaxes(0, 2) res2 = res2.swapaxes(0, 2) res = numpy.vstack([res1, res2]) res = res.swapaxes(0, 2) else: res = cv2.resize(res, dsize=(target_shape[2], target_shape[1]), interpolation=interpolation) res = res.swapaxes(0, 2) res = res.swapaxes(2, 1) if verbose: print("Shape after: ", res.shape) return res
img = imgs[i] img = cv2.imread(join(test_dir, img)).astype(np.float32) h, w, _ = img.shape img_size = h * w if sf_factor == True: lamda = h * 1.0 / w wx = int(sqrt(scale / lamda) * sf) # hx = int(lamda*wx) else: lamda = h * 1.0 / w wx = int(sqrt(scale / lamda)) # hx = int(lamda*wx) if h * w >= scale: img = cv2.resize(img, (wx, int(scale / wx))) else: if sf_factor == True: img = cv2.resize(img, (int(w * sf), int(img_size / (w * sf)))) else: pass # if h*w>=scale and h>=w: # img = cv2.resize(img, (int(img.shape[1] * max_side / img.shape[0]), max_side)) # elif h*w>=scale and h<w: # img = cv2.resize(img, (max_side, int(img.shape[0] * max_side / img.shape[1]))) if img.ndim == 2: img = img[:, :, np.newaxis] img = np.repeat(img, 3, 2) h, w, _ = img.shape
def main(): writer = SummaryWriter(opt.output) #Upsample_4x = nn.Upsample(scale_factor=4, mode='bilinear') # Build model print('Loading model ...\n') net = DnCNN(channels=1, num_of_layers=opt.num_of_layers) device_ids = [0] model = nn.DataParallel(net, device_ids=device_ids).cuda() model.load_state_dict(torch.load(os.path.join(opt.logdir, opt.net))) model.eval() # load data info print('Loading data info ...\n') files_source_A = glob.glob(os.path.join('datasets', opt.test_A, '*.*')) files_source_B = glob.glob(os.path.join('datasets', opt.test_B, '*.*')) files_source_A.sort() files_source_B.sort() # process data psnr_predict_avg = 0 psnr_defect_avg = 0 for f in range(len(files_source_A)): # image Img_A = cv2.imread(files_source_A[f]) Img_B = cv2.imread(files_source_B[f]) if opt.mode == 'S': h, w, c = Img_A.shape Img_B = cv2.resize(Img_B, (h, w), interpolation=cv2.INTER_CUBIC) Img_A = normalize(np.float32(Img_A[:,:,0])) Img_A = np.expand_dims(Img_A, 0) Img_A = np.expand_dims(Img_A, 1) Img_B = normalize(np.float32(Img_B[:,:,0])) Img_B = np.expand_dims(Img_B, 0) Img_B = np.expand_dims(Img_B, 1) I_A = torch.Tensor(Img_A) I_B = torch.Tensor(Img_B) I_A, I_B = Variable(I_A.cuda()), Variable(I_B.cuda()) with torch.no_grad(): # this can save much memory output = model(I_B) output = torch.clamp(I_B - output, 0., 1.) ## if you are using older version of PyTorch, torch.no_grad() may not be supported psnr_predict = batch_PSNR(output, I_A, 1.) psnr_predict_avg += psnr_predict psnr_defect = batch_PSNR(I_B, I_A, 1.) psnr_defect_avg += psnr_defect print("%s output psnr_predict %f" % (f, psnr_predict)) print("%s input psnr_predict %f" % (f, psnr_defect)) output= output[0,:,:].cpu() output= output[0].numpy().astype(np.float32)*255 cv2.imwrite(os.path.join(opt.output, "%#04d.png" % (f+opt.start_index)), output) psnr_predict_avg /= len(files_source_A) print("\nPSNR on output data %f" % psnr_predict_avg) psnr_defect_avg /= len(files_source_A) print("\nPSNR on input data %f" % psnr_defect_avg) I_A = I_A[0,:,:].cpu() I_A = I_A[0].numpy().astype(np.float32) I_B= I_B[0,:,:].cpu() I_B= I_B[0].numpy().astype(np.float32) fig = plt.figure() ax = plt.subplot("131") ax.imshow(I_A, cmap='gray') ax.set_title("GT") ax = plt.subplot("132") ax.imshow(I_B, cmap='gray') ax.set_title("defective input") ax = plt.subplot("133") ax.imshow(output, cmap='gray') ax.set_title("Output(DnCNN)") plt.show()
os.path.join(out_images_dir, 'org_all_imgs_{}.jpg'.format(example_index))) target_seg = target_seg.transpose(1, 2, 0) resized_imgs = resized_imgs.transpose(1, 2, 0) resized_imgs = np.array(resized_imgs) print('resized_imgs shape: {}'.format(resized_imgs.shape)) plt.imsave( os.path.join(out_images_dir, 'augment_seg_img_{}.jpg'.format(example_index)), target_seg) for i in range(configs.num_frames_sequence): img = resized_imgs[:, :, (i * 3):(i + 1) * 3] if (i == (configs.num_frames_sequence - 1)): img = cv2.resize(img, (img.shape[1], img.shape[0])) ball_img = cv2.circle(img, tuple(global_ball_pos_xy), radius=5, color=(255, 0, 0), thickness=2) ball_img = cv2.cvtColor(ball_img, cv2.COLOR_RGB2BGR) cv2.imwrite( os.path.join(out_images_dir, 'augment_img_{}.jpg'.format(example_index)), ball_img) axes[i].imshow(img) axes[i].set_title('image {}'.format(i)) fig.suptitle('Event: {}, ball_position_xy: (x= {}, y= {})'.format( event_class, global_ball_pos_xy[0], global_ball_pos_xy[1]),
def video_loop(self): """ Get frame from the video stream and show it in Tkinter """ if self.frame_no != 0 and self.start == True: try: self.index = self.img_list.index( str(self.readLogFile[-1].split('\t')[0])) + 1 self.start = False except: pass if len(self.readLogFile) != 0 and self.start == True: self.index = self.img_list.index( str(self.readLogFile[-1].split('\t')[0])) + 1 self.start = False if not self.is_paused: self.frame_no = self.img_list[self.index].split('.')[0] self.frame = cv2.imread(self.filename + '/' + self.image_folder + '/' + self.img_list[self.index]) if self.frame.size >= 640 * 360 * 3: self.mul = np.array([640, 360]) / np.array( self.frame.shape[-2::-1]) else: self.mul = [1, 1] self.global_image_frame = self.frame self.is_paused = True self.x_topleft, self.y_topleft, self.x_bottomright, self.y_bottomright, self.x_live, self.y_live = 0, 0, 0, 0, 0, 0 else: self.frame = self.global_image_frame cv2image = self.frame.copy() r, col, ch = cv2image.shape cv2resized = cv2.resize(cv2image, fx=self.mul[0], fy=self.mul[1], dsize=(0, 0)) if True: # frame captured without any errors self.panel.bind("<Button-1>", self.top_left_click) self.panel.bind("<ButtonRelease-1>", self.bottom_right_release) self.panel.bind("<B1-Motion>", self.mouse_movement) if (self.x_topleft, self.y_topleft) != (self.prev_xtl, self.prev_ytl): self.prev_xtl, self.prev_ytl = self.x_topleft, self.y_topleft self.points = [(self.prev_xtl, self.prev_ytl)] self.is_paused = True if (self.x_bottomright, self.y_bottomright) != ( self.prev_xbr, self.prev_ybr) and self.is_paused: self.prev_xbr, self.prev_ybr = self.x_bottomright, self.y_bottomright self.points += [(self.prev_xbr, self.prev_ybr)] thread1 = threading.Thread(target=self.boundingbox, args=(cv2image, self.frame_no, self.points)) thread1.start() if (self.x_live, self.y_live) != (self.prev_xl, self.prev_yl): self.prev_xl, self.prev_yl = self.x_live, self.y_live cv2.rectangle(cv2resized, (self.x_topleft, self.y_topleft), (self.x_live, self.y_live), (0, 255, 0), 1) if self.is_paused: cv2.rectangle(cv2resized, (self.x_topleft, self.y_topleft), (self.x_live, self.y_live), (0, 255, 0), 1) self.current_image = Image.fromarray( cv2resized) # convert image for PIL imgtk = ImageTk.PhotoImage( image=self.current_image) # convert image for tkinter self.panel.imgtk = imgtk # anchor imgtk so it does not be deleted by garbage-collector self.panel.config(image=imgtk) # show the image self.root.after( 40 // self.speed_ratio, self.video_loop) # call the same function after 30 milliseconds
# import the necessary packages import cv2 # load the image and show it image = cv2.imread("jurassic-park-tour-jeep.jpg") cv2.imshow("original", image) print(image.shape) # define the ratio of new image width to the old one r = 100.0 / image.shape[1] dim = (100, int(image.shape[0] * r)) # resize image resized = cv2.resize(image, dim) # , interpolation = cv2.INTER_AREA cv2.imshow("resized", resized) # grab the dimensions of the image and calculate the center of the image (h, w) = image.shape[:2] center = (w / 2, h / 2) # rotate the image by 180 degrees M = cv2.getRotationMatrix2D(center, 180, 1.0) rotated = cv2.warpAffine(image, M, (w, h)) cv2.imshow("rotated", rotated) # crop the image using array slice -- it's numpy array # after all cropped = image[70:170, 440:540] cv2.imshow("cropped", cropped) # write the cropped image to disk in png format
""" resize the images to the lowest dimension of all the images to make sure that the input for the random forest classifier are all the same """ test_dir = os.getenv("HOME") + "/test_clf_imgs/" if os.path.exists(test_dir) == False: os.makedirs(os.getenv("HOME") + "/test_clf_imgs/") for i in imgs: print(i) tmp_img = cv2.imread(i) new_w, new_h = int(lowest_dim), int(lowest_dim) resized_img = cv2.resize(tmp_img, (new_w, new_h), interpolation=cv2.INTER_AREA) # cv2.imwrite(test_dir + os.path.basename(i), resized_img) cv2.imwrite(i, resized_img) """ labels should be between 0 and 1 (not animal - animal) match labels with directories... directory order is: ['clf_data/tree', 'clf_data/pig', 'clf_data/dog', 'clf_data/flower', 'clf_data/cat', 'clf_data/skyclouds'] """
'http://ml.cs.tsinghua.edu.cn/~chenxi/dataset/val224_compressed.pkl' ] parser = argparse.ArgumentParser(description='Extract the ILSVRC2012 val dataset') parser.add_argument('--in_file', default='val224_compressed.pkl', help='input file path') parser.add_argument('--out_root', default='~/public_dataset/pytorch/imagenet-data/', help='output file path') args = parser.parse_args() d = misc.load_pickle(args.in_file) assert len(d['data']) == 50000, len(d['data']) assert len(d['target']) == 50000, len(d['target']) data224 = [] data299 = [] for img, target in tqdm.tqdm(zip(d['data'], d['target']), total=50000): img224 = misc.str2img(img) img299 = cv2.resize(img224, (299, 299)) data224.append(img224) data299.append(img299) data_dict224 = dict( data = np.array(data224).transpose(0, 3, 1, 2), target = d['target'] ) data_dict299 = dict( data = np.array(data299).transpose(0, 3, 1, 2), target = d['target'] ) if not os.path.exists(args.out_root): os.makedirs(args.out_root) misc.dump_pickle(data_dict224, os.path.join(args.out_root, 'val224.pkl')) misc.dump_pickle(data_dict299, os.path.join(args.out_root, 'val299.pkl'))
filename1 = args.img1 filename2 = args.img2 img1 = cv2.imread(filename1) img2 = cv2.imread(filename2) # take two images and enforce same size by resizing larger to smaller h1, w1, c1 = img1.shape h2, w2, c2 = img2.shape # h1 is bigger so resize h1 to h2 if h1 > h2: # calculate ratio ratio = h2 / h1 img1 = cv2.resize(img1, (0, 0), fx=ratio, fy=ratio) # resize h2 to h1 else: # calculate ratio ratio = h1 / h2 img2 = cv2.resize(img2, (0, 0), fx=ratio, fy=ratio) # convert to grayscale gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY) gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY) # find the range of pixel values min_pixel_1 = np.min(gray1) max_pixel_1 = np.max(gray1)
def mouse_event(event,x,y,flag,im): global im1_pts global im2_pts global pt_num if event == cv2.EVENT_FLAG_LBUTTON: cv2.circle(im,(x,y),2,colors[pt_num/2 % len(colors)],2) cv2.imshow("MYWIN",im) if x < im.shape[1]/2.0: im1_pts.append((x/scale_factor,y/scale_factor)) else: im2_pts.append(((x - im.shape[1]/2.0)/scale_factor,y/scale_factor)) pt_num += 1 if __name__ == '__main__': im1 = cv2.imread('frame0000.jpg') im2 = cv2.imread('frame0001.jpg') im1 = cv2.resize(im1,(int(im1.shape[1]*scale_factor),int(im1.shape[0]*scale_factor))) im2 = cv2.resize(im2,(int(im2.shape[1]*scale_factor),int(im2.shape[0]*scale_factor))) im = np.array(np.hstack((im1,im2))) cv2.imshow("MYWIN",im) cv2.setMouseCallback("MYWIN",mouse_event,im) while True: if len(im2_pts) >= 12: break cv2.waitKey(50) f = open('correspondences.pickle','wb') pickle.dump((im1_pts,im2_pts),f) f.close() cv2.destroyAllWindows()
def main(): """Create the model and start the evaluation process.""" global args args = get_arguments() os.environ["CUDA_VISIBLE_DEVICES"] = '0' # hourglass_imag_pl = tf.placeholder(tf.float32, (1, 240, 320, 3)) # input_height = 384 # input_width = 512 input_height = 240 input_width = 320 imag_pl = tf.compat.v1.placeholder(tf.float32, (1, input_height, input_width, 3)) #imag_pl = tf.placeholder(tf.float32, (None, input_height, input_width, 3)) mega_out = build_mega_model(imag_pl) # Set up TF session and initialize variables. config = tf.compat.v1.ConfigProto() config.gpu_options.per_process_gpu_memory_fraction = 0.8 sess = tf.compat.v1.Session(config=config) init = tf.compat.v1.global_variables_initializer() sess.run(init) # Load weights. model_restore_var = [v for v in tf.compat.v1.global_variables()] loader = tf.compat.v1.train.Saver(var_list=model_restore_var) load(loader, sess, MEGA_MODEL_WEIGHTS) ############################################################################### print('-------------------Single input------------------------------') img_path = '../doc/demo.jpg' print(img_path) ############## pre-processing ############### # img = np.float32(io.imread(img_path)) / 255.0 ############## pre-processing ############### # turn on when on pre-processing img = np.float32(io.imread(img_path)) w, h = img.shape[:2] # turn on when on pre-processing # img = resize(img, (input_height, input_width), order=1) img = cv2.resize(img, (input_width, input_height)) img = np.expand_dims(img, axis=0) j = 0 for k in range(10): start_time = time.time() depth = sess.run(mega_out, feed_dict={imag_pl: img}) duration = time.time() - start_time print('duration_' + str(j) + ':', duration) j = j + 1 depth = np.squeeze(depth) # (1, 384, 512, 1) pred_inv_depth = depth ############################################################################### ############################### post ########################## # depth = np.exp(depth) # # # np.save(file='demo_tf', arr=depth) # # pred_inv_depth = 1 / depth # pred_inv_depth = pred_inv_depth / np.amax(pred_inv_depth) # pred_inv_depth = cv2.resize(pred_inv_depth, (h, w)) # ############################################################ # pred_inv_depth = cv2.resize(depth, (h, w)) print('----------------- output path --------------------') output_path = 'hell0_demo_tf_320x240_prepost.png' #pred_inv_depth = depth io.imsave(output_path, pred_inv_depth) print('Output is:', output_path) # for i in range(16): # input_buff_list.append(input_image) # input_batch_ori = np.stack(input_buff_list) # # start_time = time.time() # depth = sess.run(fusion_out, # feed_dict={imag_pl:input_batch_ori}) # duration1 = time.time() - start_time # print('ckpt_duration1', duration1) # # # # time profile # for i in range(11): # i=i+1 # start_time = time.time() # depth = sess.run(fusion_out, # feed_dict={imag_pl: input_batch_ori}) # duration2 = time.time() - start_time # print('ckpt_duration_per_frame_' + str(i)+':', duration2/16) # # # depth = depth[0, :, :, 0] # # depth = normalization_0255(depth.max() - depth) # # out_img = hist.HistogramMapping(depth) # # out_img = cv2.resize(out_img, (h, w)) # for i in range(16): # cv2.imwrite('fusion_out_img_'+ str(i) +'.png', depth[i]) print('Done Here!!')
all_data = [] for path in imagePaths: company = '' for c in companies: if c in path: company = c all_data.append({'comp': c, 'path': path}) print(all_data) u = Utility() for image in all_data: try: p1 = cv2.imread(image['path']) p1 = cv2.resize(p1, (300, 300)) p1 = cv2.cvtColor(p1, cv2.COLOR_BGR2GRAY) for i in all_data: if i['path'] != image['path']: p2 = cv2.imread(i['path']) p2 = cv2.resize(p2, (300, 300)) p2 = cv2.cvtColor(p2, cv2.COLOR_BGR2GRAY) u.compare_images(image, i, p1, p2) print( "{total} images has been searched. {found} images found by over {tr} treshold." .format(u)) except Exception as e: print(str(e))
def start_app(cnn): boxes = {} columns = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V','W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM' ] clm = 1 frcnt = 0 face_cnt = 0 erf = 4 #excel row where frame count starts. book = Workbook() sheet = book.active sheet['A1'] = "frameNo" while cap.isOpened(): faces, fr, gray_fr = getdata() cv2.putText(fr, "Frame " + str(frcnt), (20, 30), cv2.FONT_HERSHEY_PLAIN, 2, (255, 255, 0), 2) sheet['A'+str(frcnt+4)] = frcnt if len(boxes) == 0: for (x, y, w, h) in faces: crop_face = fr[y:y + h, x:x + w] x_o, y_o, w_o, h_o = x - 0, y - 0, x + w + 0, y + h + 0 name = 'face'+str(face_cnt) boxes[(x_o, y_o, w_o, h_o)] = {"Frustrated": 0, "Disagree": 0, "Tense": 0, "Happy": 0, "Surprise": 0, "Neutral": 0, "show_img":crop_face,"face_name":name} sheet[columns[clm]+ str(1)] = str((x_o, y_o, w_o, h_o)) sheet[columns[clm] + str(2)] = name cv2.imwrite(name+'.png',crop_face) img_in_sheet = Image(name+'.png') sheet.add_image(img_in_sheet,columns[clm] + str(3) ) clm+=1 face_cnt += 1 else: # pass for (x, y, w, h) in faces: isNew = True crop_face = fr[y:y + h, x:x + w] x_o, y_o, w_o, h_o = x - 0, y - 0, x + w + 0, y + h + 0 box_length = len(boxes) for (p, q, r, s) in boxes: ifo = p_new, q_new, r_new, s_new = p - 25, q - 25, p + 25, q + 25 if (overlap((x_o, y_o, w_o, h_o),(p,q,r,s))): if(areaOfIntersection((x_o, y_o, w_o, h_o),(p,q,r,s)) > 20): isNew = False # print(compared_faces) if (isNew): name = 'face' + str(face_cnt) boxes[(x_o, y_o, w_o, h_o)] = {"Frustrated": 0, "Disagree": 0, "Tense": 0, "Happy": 0, "Surprise": 0, "Neutral": 0, "show_img":crop_face,"face_name":name} sheet[columns[clm]+str(1)] = str((x_o, y_o, w_o, h_o)) sheet[columns[clm] + str(2)] = name cv2.imwrite(name + '.png', crop_face) img_in_sheet = Image(name + '.png') sheet.add_image(img_in_sheet, columns[clm] + str(3)) clm += 1 face_cnt+=1 for each in boxes: cv2.rectangle(fr, (each[0], each[1]), (each[2], each[3]), (0, 255, 0), 1) cv2.putText(fr, boxes[each]['face_name'], (each[2]-40, each[3]+20 ), cv2.FONT_HERSHEY_PLAIN, 1, (0, 255, 255), 1) # cv2.rectangle(fr, (each[0] - 25, each[1] - 25), (each[0] + 25, each[1] + 25), (0, 0, 255), 1) for (x, y, w, h) in faces: face = (x,y,w,h) fc = gray_fr[y:y + h, x:x + w] roi = cv2.resize(fc, (48, 48)) pred = cnn.predict_emotion(roi[np.newaxis, :, :, np.newaxis]) # print(pred) # print(frcnt) # print(face) face_new = face[0], face[1], face[0] + face[2], face[1] + face[3] for a_face in boxes: if overlap(face_new,a_face): for ln in range(len(boxes)): if(sheet[columns[ln+1]+str(2)].value == boxes[a_face]["face_name"]): sheet[columns[ln+1]+str(erf)] = pred # if pred == "Happy": # boxes[a_face]["Happy"] += 1 # if pred == "Frustrated": # boxes[a_face]["Frustrated"] += 1 # if pred == "Disagree": # boxes[a_face]["Disagree"] += 1 # if pred == "Tense": # boxes[a_face]["Tense"] += 1 # if pred == "Surprise": # boxes[a_face]["Surprise"] += 1 # if pred == "Neutral": # boxes[a_face]["Neutral"] += 1 cv2.putText(fr, pred, (x, y - 10), cv2.FONT_HERSHEY_PLAIN, 1, (0, 255, 255), 1) cv2.rectangle(fr, (x, y), (x + w, y + h), (255, 255, 0), 1) if cv2.waitKey(1) == 27: break # cv2.imwrite('newdir/' + 'frame' + str(frcnt) + '.jpg', fr) cv2.imshow('Facial Emotion Recognition', fr) frcnt += 1 erf +=1 with open('res.txt','w') as res_file: for key in boxes.keys(): res_file.write("%s,%s\n" % (key, boxes[key])) # if frcnt == 25: # with open('facesRecorded.txt', 'w') as text_file: # text_file.write("Faces detected in frame"+str(frcnt)+ ":\n") # for (a1,b1,c1,d1) in faces: # each_face = (a1,b1,c1+a1,d1+b1) # text_file.write(str(each_face)+",\n") # text_file.write("###############################\n") # text_file.write("Originally Saved Faces:" +"\n") # for rec_face in boxes: # text_file.write(str(rec_face)+",\n") # name = 0 # for every_face in boxes: # filename = 'face'+str(name)+'.png' # face_img = boxes[every_face]['show_img'] # cv2.imwrite(filename,face_img) # name+=1 book.save("recorded_face_data.xlsx") cap.release() cv2.destroyAllWindows()
def keras_process_image(img): image_x = 64 image_y = 64 img = cv2.resize(img, (3,64,64), interpolation = cv2.INTER_AREA) return img
# We load the xml file classifier = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') # Above line normalTest #classifier = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml') #Above line test with different calulation #classifier = cv2.CascadeClassifier('haarcascade_frontalface_alt_tree.xml') #classifier = cv2.CascadeClassifier('lbpcascade_frontalface.xml') while True: i+=1 (rval, im) = webcam.read() im=cv2.flip(im,1,0) #Flip to act as a mirror # Resize the image to speed up detection mini = cv2.resize(im, (int(im.shape[1] / size), int(im.shape[0] / size))) # detect MultiScale / faces faces = classifier.detectMultiScale(mini) def run(index): test_image = image.load_img( '/unknownfaces/face.jpg', target_size = (128, 128)) test_image = image.img_to_array(test_image) test_image = np.expand_dims(test_image, axis=0) r=model.predict(test_image) q=["1","2","3","4"] return(q[r[0].tolist().index(max(r[0]))]) # Draw rectangles around each face for f in faces:
inpBlob = cv2.dnn.blobFromImage(frame, 1.0 / 255, (inWidth, inHeight), (0, 0, 0), swapRB=False, crop=False) net.setInput(inpBlob) output = net.forward() print("forward = {}".format(time.time() - t)) # Empty list to store the detected keypoints points = [] for i in range(nPoints): # confidence map of corresponding body's part. probMap = output[0, i, :, :] probMap = cv2.resize(probMap, (frameWidth, frameHeight)) # Find global maxima of the probMap. minVal, prob, minLoc, point = cv2.minMaxLoc(probMap) if prob > threshold : cv2.circle(frameCopy, (int(point[0]), int(point[1])), 6, (0, 255, 255), thickness=-1, lineType=cv2.FILLED) cv2.putText(frameCopy, "{}".format(i), (int(point[0]), int(point[1])), cv2.FONT_HERSHEY_SIMPLEX, .8, (0, 0, 255), 2, lineType=cv2.LINE_AA) # Add the point to the list if the probability is greater than the threshold points.append((int(point[0]), int(point[1]))) else : points.append(None) # Draw Skeleton for pair in POSE_PAIRS:
peds= ped_cascade.detectMultiScale(gray,1.3,2) for (b1x,b1y,b1w,b1h) in buses: cv2.rectangle(img,(b1x,b1y),(b1x+b1w,b1y+b1h),(0,150,255),2) font=cv2.FONT_HERSHEY_DUPLEX cv2.putText(img,'bus',(b1x,b1y),font,1,(255,255,255),3,cv2.LINE_8) for (x,y,w,h) in cars: cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,150),2) font=cv2.FONT_HERSHEY_DUPLEX cv2.putText(img,'car',(x,y),font,1,(255,255,255),3,cv2.LINE_8) for (bx,by,bw,bh) in bikes: cv2.rectangle(img,(bx,by),(bx+bw,by+bh),(0,0,0),2) font=cv2.FONT_HERSHEY_DUPLEX cv2.putText(img,'two_wheeler',(bx,by),font,1,(255,255,255),3,cv2.LINE_8) for (px,py,pw,ph) in peds: cv2.rectangle(img,(px,py),(px+pw,py+ph),(0,0,255),2) font=cv2.FONT_HERSHEY_DUPLEX cv2.putText(img,'person',(px,py),font,1,(255,255,255),3,cv2.LINE_8) img=cv2.resize(img,(1000,700)) cv2.imshow('video', img) if cv2.waitKey(33) == 27: break cv2.destroyAllWindows()
pickle.dump(known_names, fp) with open("known_faces.txt", "rb") as fp: # Unpickling known_faces = pickle.load(fp) with open("known_names.txt", "rb") as fp: # Unpickling known_names = pickle.load(fp) print(known_names) ''' print("processing unknown faces") while True: ret, image = video.read() scale_percent = 90 # percent of original size width = int(image.shape[1] * scale_percent / 100) height = int(image.shape[0] * scale_percent / 100) dim = (width, height) # resize image image = cv2.resize(image, dim, interpolation=cv2.INTER_AREA) locations = face_recognition.face_locations(image, model=Model) encodings = face_recognition.face_encodings(image, locations) #image=cv2.cvtColor(image,cv2.COLOR_RGB2BGR) for face_encoding, face_location in zip(encodings, locations): results = face_recognition.compare_faces(known_faces, face_encoding, Tolerance) resu1t1 = face_recognition.face_distance(known_faces, face_encoding) match = None if True in results: print(results) match = known_names[results.index(True)] print(f"Match found: {match}") else: match = str(next_id)
def evaluation(Discriminator): alfred = glob.glob("/Users/Erling/Documents/PaintingGAN/test/Alfred_Sisley/*") leonardo = glob.glob("/Users/Erling/Documents/PaintingGAN/test/Leonardo_da_Vinci/*") pablo = glob.glob("/Users/Erling/Documents/PaintingGAN/test/Pablo_Picasso/*") rembrandt = glob.glob("/Users/Erling/Documents/PaintingGAN/test/Rembrandt/*") groups = ["alfred", "leonardo", "pablo", "rembrandt"] list_of_paths = {"alfred": alfred, "leonardo": leonardo, "pablo": pablo, "rembrandt": rembrandt} size= 512, 512 counter = 0 batch_images = {} batch_labels = {} error_results = {} for group in groups: for imagePath in list_of_paths[group]: # load the image, pre-process it, and store it in the data list im = Image.open(imagePath) im.thumbnail(size, Image.ANTIALIAS) im = np.array(im) im = cv2.resize(im, (512, 512)) im = torch.from_numpy(im) im = im.transpose(0,-1) im = im[None, :, :] while im.size() != torch.Size([1, 3, 512, 512]): imagePath = np.random.choice(list_of_paths[group]) # load the image, pre-process it, and store it in the data list im = Image.open(imagePath) im.thumbnail(size, Image.ANTIALIAS) im = np.array(im) im = cv2.resize(im, (512, 512)) im = torch.from_numpy(im) im = im.transpose(0,-1) im = im[None, :, :] label = np.zeros(4) if group == "alfred": label[0] = 1 elif group == "leonardo": label[1] = 1 elif group == "pablo": label[2] = 1 elif group == "rembrandt": label[3] = 1 batch_images.update({str(counter): im}) batch_labels.update({str(counter): label}) counter+= 1 print("Cross validation:") correct = 0 total = 0 alfred_error = 0 leo_error = 0 pablo_error = 0 rembrandt_error = 0 for i in range(16*4): output = DClassifier.model(batch_images[str(i)]).detach() predicted = np.argmax(output) label = batch_labels[str(i)] label = np.argmax(torch.Tensor([label])) if predicted == label: correct += 1 else: if label == 0: alfred_error +=1 error_results.update({("alfred" + str(alfred_error)) : output } ) elif label == 1: leo_error+=1 error_results.update({("Da Vinci" + str(leo_error)) : output } ) elif label == 2: pablo_error +=1 error_results.update({("Picasso" + str(pablo_error)) : output } ) elif label == 3: rembrandt_error += 1 error_results.update({("Rembrandt" + str(rembrandt_error)) : output } ) total += 1 print("Cross validation:",correct/(total)) print("The agent managed", correct, "out of a total of:", total) print("Errors in Alfred images:", alfred_error, "out of 16") print("Errors in Da Vinci images:", leo_error, "out of 16") print("Errors in Picasso images:", pablo_error, "out of 16") print("Errors in Rembrandt images:", rembrandt_error, "out of 16") print ("-----------------------ERRORS-----------------------") for error in error_results: print(error ,":", error_results[error])
def rescale_frame(frame, percent= 100): width = int(frame.shape[1] * percent/ 100) height = int(frame.shape[0] * percent/ 100) dim = (width, height) return cv2.resize(frame, dim, interpolation =cv2.INTER_AREA)
data = json.loads(f.read()) while frame_got: frame_got, frame = cam.read() print('Frame', frame) facebox = mark_detector.extract_cnn_facebox(frame) print('Facebox', facebox) height, width = frame.shape[:2] pose_estimator = PoseEstimator(img_size=(height, width)) # cv2.line(frame, (50, 30), (450, 35), (255, 0, 0), thickness=5) face_img = frame[facebox[1]:facebox[3], facebox[0]:facebox[2]] face_img = cv2.resize(face_img, (CNN_INPUT_SIZE, CNN_INPUT_SIZE)) face_img = cv2.cvtColor(face_img, cv2.COLOR_BGR2RGB) # marks = mark_detector.detect_marks(face_img) # print('Marks', marks) for v in tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES): print('=' * 30) print(v) print(v.name) print('=' * 30) v.load(data[v.name], sess) print('FFFFace', face_img) predictions = sess.run(logits, feed_dict={inputs: [face_img]}) # Convert predictions to landmarks.
#!/usr/bin/python2 import sys import cv2 import pickle as pickle import numpy as np import localcrop import rectify if sys.argv[1].find(".pkl") != -1: src = np.array(pickle.load(open(sys.argv[1], "rb"))) else: src = cv2.imread(sys.argv[1]) rec = rectify.Rectify() dst = rec.rectify(src) # override for ROI (x, y, w, h, yoffset) = localcrop.params() crp = dst[y + yoffset:(y + h) + yoffset, x:(x + w)] sml = cv2.resize(crp, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_AREA) cv2.rectangle(dst, (x, y + yoffset), (x + w - 1, y + yoffset + h - 1), (255, 0, 255)) cv2.imshow('resized', sml) cv2.imshow('cropped', crp) cv2.imshow('rectified', dst) cv2.waitKey(0) cv2.destroyAllWindows()
dst = np.zeros(img_gray.shape) # Add the thick boundary lines to the image using 'AND' operator dst = cv2.bitwise_and(img_output, img_output, mask=mask) return dst if 0: cap = cv2.VideoCapture(0) cur_char = -1 prev_char = -1 while True: ret, frame = cap.read() frame = cv2.resize(frame, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_AREA) c = cv2.waitKey(1) if c == 27: break if c > -1 and c != prev_char: cur_char = c prev_char = c if cur_char == ord('s'): cv2.imshow('Cartoonize', cartoonize_image(frame, sketch_mode=True)) elif cur_char == ord('c'): cv2.imshow('Cartoonize', cartoonize_image(frame, sketch_mode=False)) else: cv2.imshow('Cartoonize', frame)
break ''' frame = cv2.imread("2.jpeg") cv2.imshow("frame", frame) cv2.waitKey(0) # frame = imutils.resize(frame, width=300) # frame = cv2.resize(frame, (100, 100), interpolation=cv2.INTER_CUBIC) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) frameClone = frame.copy() rects = detector.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30), flags=cv2.CASCADE_SCALE_IMAGE) for (fx, fy, fw, fh) in rects: roi = gray[fy:fy+fh, fx:fx+fw] roi = cv2.resize(roi, (28, 28)) roi = roi.astype("float") / 255.0 roi = img_to_array(roi) roi = np.expand_dims(roi, axis=0) (notSmiling, smiling) = model.predict(roi)[0] label = "Smiling" if smiling > notSmiling else "Not Smiling" cv2.putText(frameClone, label, (fx, fy-10), cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 255, 0), 2) cv2.rectangle(frameClone, (fx, fy), (fx+fw, fy+fh), (0, 255, 0), 2) cv2.imshow("Face", frameClone) cv2.waitKey(0) cv2.destroyAllWindows()
height = int(frame.shape[0] * percent/ 100) dim = (width, height) return cv2.resize(frame, dim, interpolation =cv2.INTER_AREA) def change_res(width, height): cap.set(3, width) cap.set(4, height) width = 1280 height = 720 w1=0 w2=100 h1=1100 h2=1280 clc = cv2.imread('Clear-1.jpg') clc = cv2.resize(clc,(h2-h1,w2-w1), interpolation =cv2.INTER_AREA) cap = cv2.VideoCapture(1) change_res(width,height) cv2.namedWindow('opened') cv2.createTrackbar('black', 'opened', 170, 255, nothing) cv2.createTrackbar('area_max', 'opened', 600, 5000, nothing) cv2.createTrackbar('area_min', 'opened', 10, 500, nothing) cv2.createTrackbar('radii', 'opened',2000,3000, nothing) kernel = np.ones((5, 5), np.uint8) center = [] ret, frame = cap.read() rows, cols, ch = frame.shape screen = np.zeros((rows,cols, ch), np.uint8) screen[w1:w2, h1:h2] = clc c = 0 while (1):
def main(): cap = cv2.VideoCapture(0) cap.set(3, 50) cap.set(4, 50) (width, height) = (cap.get(3), cap.get(4)) pygame.init() drum = pygame.mixer.Sound("drum.wav") drum2 = pygame.mixer.Sound("drum2.wav") drum.set_volume(0.3) soundR = False soundG = False r = 7 print(f'{width}x{height}') while True: ret, frame = cap.read() frame = cv2.flip(frame, 1) hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) p1 = (0, int(4 * height / 5)) p2 = (int(width), int(height)) cv2.rectangle(frame, p1, p2, (0, 0, 255), 1) # Red low = np.array([140, 150, 50]) high = np.array([180, 255, 255]) imgMask = cv2.inRange(hsv, low, high) count = 0 (xr, yr) = (0, 0) for i, _ in enumerate(imgMask): for j, c in enumerate(_): if (imgMask[i, j] == 255): xr += j yr += i count += 1 if count != 0: xr /= count yr /= count cv2.circle(frame, (int(xr), int(yr)), r, (255, 0, 255), -1) # Green low = np.array([40, 50, 50]) high = np.array([80, 255, 255]) imgMask = cv2.inRange(hsv, low, high) count = 0 (xg, yg) = (0, 0) for i, _ in enumerate(imgMask): for j, c in enumerate(_): if (imgMask[i, j] == 255): xg += j yg += i count += 1 if count != 0: xg /= count yg /= count cv2.circle(frame, (int(xg), int(yg)), r, (0, 0, 255), -1) if yr > p1[1] - r: if not soundR: drum.play() soundR = True else: soundR = False if yg > p1[1] - r: if not soundG: drum2.play() soundG = True else: soundG = False frame = cv2.resize(frame, None, fx=5, fy=5, interpolation=cv2.INTER_CUBIC) cv2.imshow(f'{window} Original', frame) if cv2.waitKey(1) == 27: break cap.release() cv2.destroyAllWindows()
import cv2 img = cv2.imread('lenna.png', cv2.IMREAD_COLOR) maxsize = (32, 32) resizedImg = cv2.resize(img, maxsize, interpolation=cv2.INTER_AREA) filetxt = open("lenna.txt", 'w') for row in resizedImg: for pixel in row: R = float((float(pixel[0]) / 256) * 7) G = float((float(pixel[1]) / 256) * 7) B = float((float(pixel[2]) / 256) * 3) #print(pixel[2], B, "\t{0:03b}, {0:03b}, {02:02b}".format(int(R), int(G), int(B))) filetxt.write("{0:03b}{0:03b}{02:02b}".format(int(R), int(G), int(B))) filetxt = open("lenna.txt", 'r') binarytxt = filetxt.read() filebin = open("lenna.bin", 'bw') binaryimg = bytearray( [int(binarytxt[i:i + 8], 2) for i in range(0, len(binarytxt), 8)]) filebin.write(binaryimg) filebin.close() filetxt.close()
# In[22]: sct = mss() monitor = {"top": 40, "left": 0, "width": 1024, "height": 768} with detection_graph.as_default(): with tf.Session(graph=detection_graph) as sess: while True: #live monitor capture last_time = time.time() sct.get_pixels(monitor) img = Image.frombytes('RGB', (sct.width, sct.height), sct.image) image_np = np.array(img) image_np = cv2.resize(image_np, (1024, 768)) # image = Image.open(image_path) # image_np = load_image_into_numpy_array(image) # Expand dimensions since the model expects images to have shape: [1, None, None, 3] image_np_expanded = np.expand_dims(image_np, axis=0) image_tensor = detection_graph.get_tensor_by_name('image_tensor:0') # Each box represents a part of the image where a particular object was detected. boxes = detection_graph.get_tensor_by_name('detection_boxes:0') # Each score represent how level of confidence for each of the objects. # Score is shown on the result image, together with the class label. scores = detection_graph.get_tensor_by_name('detection_scores:0') classes = detection_graph.get_tensor_by_name('detection_classes:0') num_detections = detection_graph.get_tensor_by_name('num_detections:0') # Actual detection. (boxes, scores, classes, num_detections) = sess.run(
face_recognizer = cv2.face.LBPHFaceRecognizer_create() face_recognizer.read('trainingData.yml') name = {0: "unknown", 1: "Hemant"} cap = cv2.VideoCapture(0) #capture image from web cam while True: ret, test_img = cap.read() faces_detected, gray_img = fr.faceDetection(test_img) for (x, y, w, h) in faces_detected: cv2.rectangle(test_img, (x, y), (x + w, y + h), (255, 0, 0), thickness=6) resized_img = cv2.resize(test_img, (1000, 700)) cv2.imshow('FAce detection tutorial', resized_img) cv2.waitKey(10) for face in faces_detected: (x, y, w, h) = face roi_gray = gray_img[y:y + w, x:x + h] label, confidence = face_recognizer.predict(roi_gray) print(confidence, label) fr.draw_rect(test_img, face) predicted_name = name[label] if confidence < 60: fr.put_text(test_img, predicted_name, x, y) resized_img = cv2.resize(test_img, (1000, 700)) cv2.imshow("FaceREcognition", resized_img)
BS = 8 EPOCHS = 50 # grab the list of images in our dataset directory, then initialize # the list of data (i.e., images) and class images print("[INFO] loading images...") imagePaths = list(paths.list_images(DATASET_DIR)) data = [] labels = [] for imagePath in imagePaths: # extract the class label from the filename, load the image and # resize it to be a fixed 32x32 pixels, ignoring aspect ratio label = imagePath.split(os.path.sep)[-2] image = cv2.imread(imagePath) image = cv2.resize(image, (32, 32)) # update the data and labels lists, respectively data.append(image) labels.append(label) # convert the data into a NumPy array, then preprocess it by scaling # all pixel intensities to the range [0, 1] data = np.array(data, dtype="float") / 255.0 # encode the labels (which are currently strings) as integers and then # one-hot encode them le = LabelEncoder() labels = le.fit_transform(labels) labels = np_utils.to_categorical(labels, 2)
def distort_image(cv_img, scale_params=(None, None, None), mask=None, interpolation=cv2.INTER_AREA): """ :param cv_img: opencv image :param scale_params: tuple height percentage/width percentage/rotation degree :param mask: is out parameter, returns mask of pixels to be transparent :param interpolation: INTER_AREA, INTER_CUBIC :return: image cv2 """ # first - resize width and height # if new width or height is None, then no resize if (scale_params[0] is None) and (scale_params[1] is None): res = cv_img.copy() else: fy = 1. if not scale_params[0] else (float(scale_params[0]) / 100.0) fx = 1. if not scale_params[1] else (float(scale_params[1]) / 100.0) res = cv2.resize(cv_img.copy(), None, fx=fx, fy=fy, interpolation=interpolation ) # INTER_AREA is faster then INTER_CUBIC # if no rotation if not scale_params[2]: return res #, res[:,:,3] if channels == 4 else np.zeros((rows, cols), np.uint8) # to avoid missing corners after rotation - image should be expanded rows, cols = res.shape[:2] img_ext = res.copy() radius = int(math.ceil( math.sqrt(rows * rows + cols * cols) / 2.0)) # transformed image definitely in circle with that radius lr_border, tb_border = radius - int(float(cols) / 2.0), radius - int( float(rows) / 2.0) # extending with borders img_ext = cv2.copyMakeBorder(img_ext, tb_border, tb_border, lr_border, lr_border, cv2.BORDER_CONSTANT, img_ext, (0, 0, 0)) rows_ext, cols_ext = img_ext.shape[:2] # mask creating mask_ = np.zeros((rows_ext, cols_ext), np.uint8) cv2.rectangle(mask_, (lr_border, tb_border), (cols_ext - lr_border, rows_ext - tb_border), 255, -1) # second - apply rotation # rotation matrix creation M = cv2.getRotationMatrix2D((cols_ext / 2, rows_ext / 2), scale_params[2], 1) # affine transform dst = cv2.warpAffine( img_ext, M, (cols_ext, rows_ext)) # borderMode=cv2.BORDER_TRANSPARENT, borderValue=0 # mask can be not rectangle, if input image was with transparency if dst.shape[2] == 4: mask_ = dst[:, :, 3] else: mask_ = cv2.warpAffine(mask_, M, (cols_ext, rows_ext)) mask = mask_ return dst