def draw_vertical_line(self,line,color): starting_dot_pos=self.dot_locations[line[0][0]][line[0][1]] ending_dot_pos=self.dot_locations[line[1][0]][line[1][1]] line_top_left_pos=(starting_dot_pos[0]-self.dot_radius,starting_dot_pos[1]+self.dot_radius+1) line_bottom_right_pos=(ending_dot_pos[0]+self.dot_radius,ending_dot_pos[1]-self.dot_radius-1) cv2.rectangle(self.grid, line_top_left_pos, line_bottom_right_pos, color, -1) return (line_top_left_pos,line_bottom_right_pos)
def template_matching(): img = cv2.imread('messi.jpg',0) img2 = img.copy() template = cv2.imread('face.png',0) w, h = template.shape[::-1] # All the 6 methods for comparison in a list methods = ['cv2.TM_CCOEFF', 'cv2.TM_CCOEFF_NORMED', 'cv2.TM_CCORR', 'cv2.TM_CCORR_NORMED', 'cv2.TM_SQDIFF', 'cv2.TM_SQDIFF_NORMED'] for meth in methods: img = img2.copy() method = eval(meth) # Apply template Matching res = cv2.matchTemplate(img,template,method) min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res) # If the method is TM_SQDIFF or TM_SQDIFF_NORMED, take minimum if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]: top_left = min_loc else: top_left = max_loc bottom_right = (top_left[0] + w, top_left[1] + h) cv2.rectangle(img,top_left, bottom_right, 255, 2) plt.subplot(121),plt.imshow(res,cmap = 'gray') plt.title('Matching Result'), plt.xticks([]), plt.yticks([]) plt.subplot(122),plt.imshow(img,cmap = 'gray') plt.title('Detected Point'), plt.xticks([]), plt.yticks([]) plt.suptitle(meth) plt.show()
def draw_rectangle(src, x, y, w, h, color=(255, 0, 0), thickness=1): ''' ''' p1 = new_point(x, y, tt=True) p2 = new_point(x + w, y + h, tt=True) rectangle(src, p1, p2, convert_color(color), thickness=thickness)
def draw_line(self,line): if self.last_drawn_line_location!=None: cv2.rectangle(self.grid, self.last_drawn_line_location[0], self.last_drawn_line_location[1], self.linecolor, -1) if line[0][0]==line[1][0]: self.last_drawn_line_location=self.draw_horizontal_line(line,self.current_linecolor) else: self.last_drawn_line_location=self.draw_vertical_line(line,self.current_linecolor)
def plot_old_position_on_map(self,current_x,current_y): # Plot red box where vehicle is.... min_x=self.place_cell_id[1].min() min_y=self.place_cell_id[2].min() sq=self.squares_grid[current_x-min_x,current_y-min_y,:] cv2.rectangle(self.map_template,tuple(sq[0:2]),tuple(sq[2:4]),(0,255,0),-1) return self.map_template
def border_mask(img, p1, p2, device, debug, color="black"): # by using rectangle_mask to mask the edge of plotting regions you end up missing the border of the images by 1 pixel # This function fills this region in # note that p1 = (0,0) is the top left hand corner bottom right hand corner is p2 = (max-value(x), max-value(y)) # device = device number. Used to count steps in the pipeline # debug = True/False. If True; print output image if color=="black": ix, iy = np.shape(img) size = ix,iy bnk = np.zeros(size, dtype=np.uint8) cv2.rectangle(img = bnk, pt1 = p1, pt2 = p2, color = (255,255,255)) ret, bnk = cv2.threshold(bnk,127,255,0) contour,hierarchy = cv2.findContours(bnk,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE) cv2.drawContours(bnk, contour, -1 ,(255,255,255), 5) device +=1 if color=="gray": ix, iy = np.shape(img) size = ix,iy bnk = np.zeros(size, dtype=np.uint8) cv2.rectangle(img = bnk, pt1 = p1, pt2 = p2, color = (192,192,192)) ret, bnk = cv2.threshold(bnk,127,255,0) contour,hierarchy = cv2.findContours(bnk,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE) cv2.drawContours(bnk, contour, -1 ,(192,192,192), 5) device +=1 if debug: print_image(bnk, (str(device) + '_brd_mskd_' + '.png')) return device, bnk, contour, hierarchy
def object_detection(self, depth_array): """ Function to detect objects from the depth image given :return: """ self.detect_arm() # Perform thresholding on the image to remove all objects behind a plain ret, bin_img = cv2.threshold(depth_array, 0.3, 1, cv2.THRESH_BINARY_INV) # Erode the image a few times in order to separate close objects element = cv2.getStructuringElement(cv2.MORPH_CROSS, (3, 3)) err_img = cv2.erode(bin_img, element, iterations=20) # Create a new array of type uint8 for the findContours function con_img = np.array(err_img, dtype=np.uint8) # Find the contours of the image and then draw them on contours, hierarchy = cv2.findContours(con_img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) cv2.drawContours(con_img, contours, -1, (128, 255, 0), 3) for x in range(0, len(contours)): x, y, w, h = cv2.boundingRect(contours[x]) cv2.rectangle(con_img, (x, y), ((x+w), (y+h)), (255, 0, 127), thickness=5, lineType=8, shift=0) # Show the colour images of the objects # self.show_colour(contours) # Show the Depth image and objects images cv2.imshow('Contours', con_img) cv2.imshow("Depth", bin_img) cv2.waitKey(3)
def setLabel(im, text, contour): if showNames: labelSize = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, 0.4, 1) bx, by, bw, bh = cv2.boundingRect(contour) x, y = (bx + (bw - labelSize[0][0])/2, (by + (bh + labelSize[0][1])/2)-10) cv2.rectangle(im, (x, y), (x + labelSize[0][0], y - labelSize[0][1]), (255, 255, 255), -1) cv2.putText(im, text, (x, y), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 0, 0), 1)
def draw(x, y, w, h, image, R, G, B): global gr global go global gl cv2.rectangle(image,(x,y),(x+w,y+h),(B,G,R),2) cv2.putText(image, "Found something", (5, 220), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255)) coord = "x=%d, y=%d" % (x, y) cv2.putText(image, coord, (x, y+h+15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 128, 0)) # left = x # right = 320 - x - w # Assuming only one object will be found in each image, check if the object is centered enough # Left/right max: (horizontal window size - (rectangle width)) / 2 # leftmax = (320-width)/2 # rightmax = (320-width)/2 diff = 2 * x + w - 320 # basically diff = left-right if diff < 40 and diff >= 0: cv2.putText(image, "within range", (5, 230), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 204, 102)) go = go + 1 #left>right, so move more right elif (diff) >= 40: cv2.putText(image, "too left", (5, 230), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 204, 102)) gr = gr + 1 # diff < 0, right > left, so move more left elif (diff) < 0: cv2.putText(image, "too right", (5, 230), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 204, 102)) gl = gl + 1 else: cv2.putText(image, "nonono", (5, 230), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 204, 102))
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()
def describe(self, image): image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) features = [] (h, w) = image.shape[:2] (cX, cY) = (int(w * 0.5), int(h * 0.5)) segments = [(0, cX, 0, cY), (cX, w, 0, cY), (cX, w, cY, h), (0, cX, cY, h)] (xL, yL) = (int(w * 0.75) / 2, int(h * 0.75) / 2) elli = np.zeros(image.shape[:2], dtype = 'uint8') cv2.ellipse(elli, (cX, cY), (xL, yL), 0, 0, 360, 255, -1) for (x0, x1, y0, y1) in segments: rect = np.zeros(image.shape[:2], dtype = 'uint8') cv2.rectangle(rect, (x0, y0), (x1, y1), 255, -1) rect = cv2.subtract(rect, elli) hist = self.histogram(image, rect) features.extend(hist) hist = self.histogram(image, elli) features.extend(hist) return features
def showManyImages(title, faccia): pics = len(faccia) # number of found faces ################## r = 4 c = 2 size = (100, 100) ################## width = size[0] height = size[1] image = np.zeros((height*r, width*c, 3), np.uint8) black = (0, 0, 0) rgb_color = black color = tuple(reversed(rgb_color)) image[:] = color for i in range(0,len(faccia)): faccia[i] = cv2.resize(faccia[i], size ) cv2.rectangle(faccia[i],(0,0), size,(0,0,0),4) # face edge k=0 for i in range(0, r): for j in range(0, c): try: image[i*size[0]:i*size[0]+size[0], j*size[0]:j*size[0]+size[0]] = faccia[k] except: None k=k+1 cv2.imshow(title,image)
def __draw_superclass_result(self, img_color, regions, superclass_ids, imgs_sc): color_red = (0, 0, 255) color_blue = (255, 0, 0) color_yellow = (0, 255, 255) colors = [color_yellow, color_red, color_blue] img_width = img_color.shape[1] img_result = img_color.copy() for region, superclass_id in zip(regions, superclass_ids): x1, y1, x2, y2 = region color = colors[superclass_id] cv2.rectangle(img_result, (x1, y1), (x2, y2), color, 2) # also, we want to overlay the ground truth beside the region offset_big = 12 offset_small = 6 dim = max(y2 - y1, x2 - x1) ground_truth = imgs_sc[superclass_id] ground_truth = cv2.resize(src=ground_truth, dsize=(dim, dim), interpolation=cv2.INTER_AREA) if x1 - dim - offset_big > -1: img_result[y1:y2, x1 - dim - offset_small: x2 - dim - offset_small, :] = ground_truth elif x2 + dim + offset_big > img_width: img_result[y1:y2, x1 + dim + offset_small: x2 + dim + offset_small, :] = ground_truth return img_result
def rectangle_mask(img, p1, p2, device, debug, color="black"): # takes an input image and returns a binary image masked by a rectangular area denoted by p1 and p2 # note that p1 = (0,0) is the top left hand corner bottom right hand corner is p2 = (max-value(x), max-value(y)) # device = device number. Used to count steps in the pipeline # debug = True/False. If True; print output image # get the dimensions of the input image ix, iy = np.shape(img) size = ix,iy # create a blank image of same size bnk = np.zeros(size, dtype=np.uint8) # draw a rectangle denoted by pt1 and pt2 on the blank image if color=="black": cv2.rectangle(img = bnk, pt1 = p1, pt2 = p2, color = (255,255,255)) ret, bnk = cv2.threshold(bnk,127,255,0) contour,hierarchy = cv2.findContours(bnk,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE) # make sure entire rectangle is within (visable within) plotting region or else it will not fill with thickness = -1 # note that you should only print the first contour (contour[0]) if you want to fill with thickness = -1 # otherwise two rectangles will be drawn and the space between them will get filled cv2.drawContours(bnk, contour, 0 ,(255,255,255), -1) device +=1 if color=="gray": cv2.rectangle(img = bnk, pt1 = p1, pt2 = p2, color = (192,192,192)) ret, bnk = cv2.threshold(bnk,127,255,0) contour,hierarchy = cv2.findContours(bnk,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE) # make sure entire rectangle is within (visable within) plotting region or else it will not fill with thickness = -1 # note that you should only print the first contour (contour[0]) if you want to fill with thickness = -1 # otherwise two rectangles will be drawn and the space between them will get filled cv2.drawContours(bnk, contour, 0 ,(192,192,192), -1) if debug: print_image(bnk, (str(device) + '_roi.png')) return device, bnk, contour, hierarchy
def replaceGrayArea(frame, i, o): cv2.rectangle(frame, i, o, (250,200,0), 2) gframe = frame.copy() gframe = rgb2gray(gframe) gframe[i[1]:o[1],i[0]:o[0]] = frame[i[1]:o[1],i[0]:o[0]] return gframe
def drawObjects(frame, objects): global colors output = np.copy(frame) drawnBounds = [] for i in range(len(objects)): item = objects[i] if not item.tracked: item.kalman_filter.predict() cv2.circle(output, item.kalman_filter.currentPrediction, 8, (0,0,255), 5) continue bounds = item.bounds if bounds.center in drawnBounds: continue cv2.rectangle(output, (bounds.x1, bounds.y1), (bounds.x2, bounds.y2), colors[i % len(colors)], 1) cv2.circle(output, bounds.center, 4, colors[i % len(colors)], 2) # KALMAN # print item.kalman_filter.currentPrediction cv2.circle(output, item.kalman_filter.currentPrediction, 8, (0,0,255), 5) cv2.putText(output, 'Object ' + str(i), (bounds.x1, bounds.y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, colors[i % len(colors)], 1) drawnBounds.append(bounds.center) return output
def detect_face(MaybeImage): if MaybeImage.success: image = MaybeImage.result else: return MaybeImage # Make image grayscale for processing gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Detect faces in the image # faces will be an iterable object faces = faceCascade.detectMultiScale( image=gray_image, scaleFactor=1.1, minNeighbors=5, minSize=(40, 40), flags = cv2.cv.CV_HAAR_SCALE_IMAGE ) if len(faces) == 1: face = faces[0] return Maybe(True, face) else: # If we're run as main we can show a box around the faces. # Otherwise it's nicer if we just spit out an error message. if __name__ == '__main__': # Draw a box around the faces for (x, y, w, h) in faces: cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2) cv2.imshow("{:d} Faces found. Remove other faces. Press any key to quit.".format(len(faces)) ,image) cv2.waitKey(0) return Maybe(False, "Expected 1 face, found {:d} faces. Please make sure your face is in frame, and remove any other things detected as a face from the frame.".format(len(faces)))
def draw_box(img, x1, y1, x2, y2, color=None, thickness=1): """Draw a rectangle on the given image.""" if color is None: color = rnd_color() x1, y1, x2, y2 = map(_to_int, [x1, y1, x2, y2]) cv.rectangle(img, (x1, y1), (x2, y2), color, thickness) return img
def vis_bbox(img, bbox, thick=1): """Visualizes a bounding box.""" (x0, y0, w, h) = bbox x1, y1 = int(x0 + w), int(y0 + h) x0, y0 = int(x0), int(y0) cv2.rectangle(img, (x0, y0), (x1, y1), _GREEN, thickness=thick) return img
def onmouse(event, x, y, flags, param): global drag_start, sel if event == cv.EVENT_LBUTTONDOWN: drag_start = x, y sel = 0,0,0,0 elif event == cv.EVENT_LBUTTONUP: if sel[2] > sel[0] and sel[3] > sel[1]: patch = gray[sel[1]:sel[3],sel[0]:sel[2]] result = cv.matchTemplate(gray,patch,cv.TM_CCOEFF_NORMED) result = np.abs(result)**3 val, result = cv.threshold(result, 0.01, 0, cv.THRESH_TOZERO) result8 = cv.normalize(result,None,0,255,cv.NORM_MINMAX,cv.CV_8U) cv.imshow("result", result8) drag_start = None elif drag_start: #print flags if flags & cv.EVENT_FLAG_LBUTTON: minpos = min(drag_start[0], x), min(drag_start[1], y) maxpos = max(drag_start[0], x), max(drag_start[1], y) sel = minpos[0], minpos[1], maxpos[0], maxpos[1] img = cv.cvtColor(gray, cv.COLOR_GRAY2BGR) cv.rectangle(img, (sel[0], sel[1]), (sel[2], sel[3]), (0,255,255), 1) cv.imshow("gray", img) else: print "selection is complete" drag_start = None
def drawGrids(self, procImg): colStep = 32 rowStep = 24 for row in range(0, 480, rowStep): for col in range(0, 640, colStep): cv2.rectangle(procImg, (col, row), (col + colStep, row + rowStep), (255, 100, 0), 1) return
def update(self, frame): # print "updating %d " % self.id hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) back_project = cv2.calcBackProject([hsv],[0], self.roi_hist,[0,180],1) if args.get("algorithm") == "c": ret, self.track_window = cv2.CamShift(back_project, self.track_window, self.term_crit) pts = cv2.boxPoints(ret) pts = np.int0(pts) self.center = center(pts) cv2.polylines(frame,[pts],True, 255,1) if not args.get("algorithm") or args.get("algorithm") == "m": ret, self.track_window = cv2.meanShift(back_project, self.track_window, self.term_crit) x,y,w,h = self.track_window self.center = center([[x,y],[x+w, y],[x,y+h],[x+w, y+h]]) cv2.rectangle(frame, (x,y), (x+w, y+h), (255, 255, 0), 2) self.kalman.correct(self.center) prediction = self.kalman.predict() cv2.circle(frame, (int(prediction[0]), int(prediction[1])), 4, (255, 0, 0), -1) # fake shadow cv2.putText(frame, "ID: %d -> %s" % (self.id, self.center), (11, (self.id + 1) * 25 + 1), font, 0.6, (0, 0, 0), 1, cv2.LINE_AA) # actual info cv2.putText(frame, "ID: %d -> %s" % (self.id, self.center), (10, (self.id + 1) * 25), font, 0.6, (0, 255, 0), 1, cv2.LINE_AA)
def consume(self, frame, ROIs, ROIs_coordinates, faces): if len(ROIs) == 1: ROI_x = ROIs_coordinates[0][0] ROI_y = ROIs_coordinates[0][1] ROI_w = ROIs[0].shape[0] ROI_h = ROIs[0].shape[1] cv2.rectangle(frame, (ROI_x, ROI_y), (ROI_x+ROI_w, ROI_y+ROI_h), (0, 0, 255)) if not cv2.waitKey(1) & 0xFF == ord('p'): return frame if self.image_count == self.IMAGE_COUNT: if self.ALREADY_TRAINED: return print str(self.image_count) + " images were captured. Training recognizer ..." images, labels = self.get_images_and_labels(self.FACE_PATH) recognizer = cv2.createLBPHFaceRecognizer() self.train_recognizer(recognizer, images, labels) self.EXIT_FLAG = True return if len(faces) > 1: print "Detected more than one face. Skipping frame ..." return None if len(faces) == 0: print "No faces in the image. Skipping frame ..." return None self.image_count += 1 cv2.imwrite(self.FACE_PATH + "/" + str(self.image_count) + self.FRAMECAPTURE_EXTENSION, ROIs[0]) print "Saved image!" return frame
def draw_haar_feature(self, xml_file): cascade = cv2.CascadeClassifier(xml_file) features = cascade.detectMultiScale(self.image) for (x,y,w,h) in features: cv2.rectangle(self.drawn_img, (x,y), (x+w, y+h), (255,255,255), 3) if len(features) is 1: print 'x={0},y={1},w={2},h={3}, cascade={4}'.format(x,y,w,h,xml_file)
def show_n_write_components(img_path): if not os.path.exists(img_path): print 'Cannot find image', img_path return print('Work with %s' % img_path) image = cv2.imread(img_path) letters = FindLetters(image, debug_flags=DEFAULT_DEBUG_FLAGS ) #print json.loads(newltr) new_path = 'dumps/' + img_path.replace('/', '\\') with open(new_path, 'w') as f: for l in letters: del l['points'] del l['swvalues'] back_img = image.copy() cv2.rectangle(image, (l['Y'], l['X']), (l['Y2'], l['X2']), (0, 20, 200)) l['path'] = img_path #cv2.imshow('after all ...', image) #key = cv2.waitKey() % 0x100 #print(key) #if key != 82: # l['is_symbol'] = True # print('Marking as symbol') #else: # l['is_symbol'] = False # print('Marking as NOT symbol') newltr = json.dumps(l) print(newltr) f.write(newltr + '\n') image = back_img
def classify(image, hog, rho, max_detected=8): image_boxes = np.copy(image) found = hog.detect(image_boxes, winStride=(1, 1)) if len(found[0]) == 0: return "female", image_boxes, 0 scores = np.zeros(found[1].shape[0]) for index, score in enumerate(found[1]): scores[index] = found[1][index][0] order = np.argsort(scores) image_boxes = np.copy(image) index = 0 while index < max_detected and found[1][order[index]] - rho < 0: current = found[0][order[index], :] x, y = current h = hog.compute(image[y : (y + win_height), x : (x + win_width), :]) colour = (0, 255, 0) cv2.rectangle(image_boxes, (x, y), (x + win_width, y + win_height), colour, 1) index += 1 # print 'Number of detected objects = %d' % index return ( "male" if index > 0 else "female", image_boxes, index, found[0][order[(index - 1) : index], :], found[1][order[(index - 1) : index]], )
def image(): # Load an color image in grayscale # cv2.namedWindow('image', cv2.WINDOW_NORMAL) # print img.shape for image in glob.glob("../extras/faceScrub/download/*/*.jpg"): start = time.time() img = cv2.imread(image) # res = cv2.resize(img, (227, 227), interpolation=cv2.INTER_CUBIC) FACE_DETECTOR_PATH = "../extras/haarcascade_frontalface_default.xml" detector = cv2.CascadeClassifier(FACE_DETECTOR_PATH) rects = detector.detectMultiScale(img, scaleFactor=1.4, minNeighbors=1, minSize=(30, 30), flags=cv2.cv.CV_HAAR_SCALE_IMAGE) # gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # construct a list of bounding boxes from the detection # rects = [(int(x), int(y), int(x + w), int(y + h)) for (x, y, w, h) in rects] # update the data dictionary with the faces detected # data.update({"num_faces": len(rects), "faces": rects, "success": True}) print "time", time.time() - start for (x, y, w, h) in rects: cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2) roi_color = img[y:y + h, x:x + w] cv2.imshow('image', roi_color) cv2.waitKey(0) cv2.destroyAllWindows() cv2.imshow('image', img) cv2.waitKey(0) cv2.destroyAllWindows()
def find_hottest_points(cv_image): clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(3,3)) #gray = clahe.apply(img) gray = clahe.apply(cv_image) gray = cv2.GaussianBlur (gray, (21,21), 0) min_thresh = cv2.threshold(gray, min_th, 255, cv2.THRESH_BINARY)[1] max_thresh = cv2.threshold(gray, max_th, 255, cv2.THRESH_BINARY_INV)[1] thresh = cv2.bitwise_and(min_thresh, max_thresh) thresh = cv2.dilate(thresh, None, iterations = 2) (cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) for c in cnts: if cv2.contourArea(c) > min_area and cv2.contourArea(c) < max_area: (x,y,w,h) = cv2.boundingRect(c) # cv2.rectangle(cv_image, (x, y), (x+w, y+h), (0, 255, 0), 2) cv2.rectangle(cv_image, (x, y), (x+w, y+h), 0, 2) continue cv2.imshow("region_detector", cv_image) cv2.moveWindow("region_detector",900,0) cv2.imshow("band_threshold_image", thresh) cv2.moveWindow("band_threshold_image",900,400) cv2.waitKey(1)
def check(): cap.capture('image.bmp') img=cv2.imread('image.bmp') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) eye0 = eye0_cascade.detectMultiScale(gray, 1.3, 5) flag=0 for (x,y,w,h) in eye0: flag=1 cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2) roi_gray = gray[y:y+h, x:x+w] roi_color = img[y:y+h, x:x+w] #print roi_color if flag == 1: print 'Eye Open' tym=0 else: print 'Eye Closed' tym=tym+1 if tym >2 : print 'Next' d.rotate(1) tym=0
def draw_image(self, image, control_vect): # TODO: move this in the main script ? # Draw the motionless area color = (0, 0, 255) thickness = 1 point1 = (self.motionless_area_range_x[0], self.motionless_area_range_y[0]) point2 = (self.motionless_area_range_x[1], self.motionless_area_range_y[1]) cv.rectangle(image, point1, point2, color, thickness) # Draw the control if control_vect is not None: ctrl_text = "" if control_vect[1] > 0: ctrl_text += "up " elif control_vect[1] < 0: ctrl_text += "down " if control_vect[0] > 0: ctrl_text += "right" elif control_vect[0] < 0: ctrl_text += "left" start_point = (15, 100) font = cv.FONT_HERSHEY_SIMPLEX font_scale = 0.75 color = (0, 0, 255) thickness = 2 line_type = cv.LINE_AA # Anti-Aliased cv.putText(image, ctrl_text, start_point, font, font_scale, color, thickness, line_type)
def compare_screen_areas( self, x1, y1, x2, y2, path1, save_folder=save_folder_path, ssim=starts_ssim, image_format=starts_format_image ): """Creates a cut-out from the screen Creates a cut-out from the screen that is on the screen and compares it to a previously created x1 and y1 = x and y coordinates for the upper left corner of the square x2 and y2 = x and y coordinates for the bottom right corner of the square path1 = Path to an already created viewport with which we want to compare the viewport created by us Example: Compare screen area 0 0 25 25 ../Crop_Image1.png Creates Crop_Image1.png from 0, 0, 25, 25 """ self._check_dir(save_folder) self._check_ssim(ssim) self._check_image_format(image_format) save_folder = self.save_folder self.seleniumlib.capture_page_screenshot(save_folder + '/test1.png') path2 = save_folder + '/test1.png' if os.path.exists(path1): if os.path.exists(path2): # load img img1 = cv.imread(path1, 1) # img from docu img2 = cv.imread(path2, 1) # img from screenshot # convert to grey gray_img1 = cv.cvtColor(img1, cv.COLOR_BGR2GRAY) gray_img2 = cv.cvtColor(img2, cv.COLOR_BGR2GRAY) # spliting area crop_img = gray_img2[ int(x1): int(y2), int(y1): int(x2) ] # Crop from {x, y, w, h } => {0, 0, 300, 400} # SSIM diff img (self.score, diff) = structural_similarity( gray_img1, crop_img, full=True ) diff = (diff * 255).astype('uint8') # Threshold diff img thresh = cv.threshold( diff, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU )[1] cnts = cv.findContours( thresh.copy(), cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE ) cnts = imutils.grab_contours(cnts) crop_img_color = img2[int(x1): int(y2), int(y1): int(x2)] # Create frame in diff area for c in cnts: (x, y, w, h) = cv.boundingRect(c) cv.rectangle(img1, (x, y), (x + w, y + h), (0, 0, 255), 2) cv.rectangle(crop_img_color, (x, y), (x + w, y + h), (0, 0, 255), 2) # Show image if float(self.score) < self.ssim: self.robotlib = BuiltIn().get_library_instance('BuiltIn') img_diff = cv.hconcat([img1, crop_img_color]) time_ = str(time.time()) self.seleniumlib.capture_page_screenshot( save_folder + '/img' + time_ + '.png' ) cv.imwrite(save_folder + '/img' + time_ + self.format, img_diff) self.robotlib.fail('Image has diff: {} '.format(self.score)) score_percen = float(self.score) * +100 self.robotlib.fail('Image has diff: {} %'.format(score_percen)) else: img_diff = cv.hconcat([self.img1, self.img2]) time_ = str(time.time()) self.seleniumlib.capture_page_screenshot( save_folder + "/Img" + time_ + self.format ) cv.imwrite(save_folder + "/Img" + time_ + self.format, img_diff) self.robotlib.log_to_console( "Image has diff: {} ".format(self.score) ) else: raise AssertionError("New screen doesnt exist anymore") else: raise AssertionError("The path1 to the image does not exist. Try a other path, than:" + path1) if os.path.exists(save_folder + '/test1.png'): os.remove(save_folder + '/test1.png')
# Find contours (_, contours, _) = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) print("Found %d components." % len(contours)) centroids = [] G = nx.Graph() tempInt = 0 for c in contours: M = cv2.moments(c) temp = [] temp.append(int(M["m10"] / M["m00"])) temp.append(int(M["m01"] / M["m00"])) print(M["m00"]) x,y,w,h = cv2.boundingRect(c) cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,0),2) print(x,y,w,h) cropped = image[x:w,y:h] #cv2.imshow("cropped",cropped) #cv2.waitKey(0) centroids.append(temp) # Adding nodes to the graph with their attributes G.add_node(tempInt, pos = temp) tempInt = tempInt + 1 G = Graph_EdgesConstruction(centroids, G, 130.0) Graph = nx.to_numpy_matrix(G) print("Graph: ") print(Graph)
name = "Unknown" # If a match was found in known_face_encodings, just use the first one. # if True in matches: # first_match_index = matches.index(True) # name = known_face_names[first_match_index] # Or instead, use the known face with the smallest distance to the new face face_distances = face_recognition.face_distance(known_face_encodings, face_encoding) best_match_index = np.argmin(face_distances) if matches[best_match_index]: name = known_face_names[best_match_index] # Draw a box around the face cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2) # Draw a label with a name below the face cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED) font = cv2.FONT_HERSHEY_DUPLEX cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1) # Display the resulting image cv2.imshow('Video', frame) # Hit 'q' on the keyboard to quit! if cv2.waitKey(1) & 0xFF == ord('q'): break # Release handle to the webcam video_capture.release()
def __find_lane_pixels(self, binary_warped): # Input needs to be a single channel image # Take a histogram of the bottom half of the image histogram = np.sum(binary_warped[binary_warped.shape[0] // 2:, :], axis=0) # Create a blank image to draw the rectangles on out_img = np.zeros((binary_warped.shape[0], binary_warped.shape[1], 3), np.uint8) # Find the peak of the left and right halves of the histogram # These will be the starting point for the left and right lines midpoint = np.int(histogram.shape[0] // 2) leftx_base = np.argmax(histogram[:midpoint]) rightx_base = np.argmax(histogram[midpoint:]) + midpoint # Set height of windows - based on nwindows above and image shape window_height = np.int(binary_warped.shape[0] // self.__nwindows) # Identify the x and y positions of all nonzero pixels in the image nonzero = binary_warped.nonzero() nonzeroy = np.array(nonzero[0]) nonzerox = np.array(nonzero[1]) # Current positions to be updated later for each window in nwindows leftx_current = leftx_base rightx_current = rightx_base # Create empty lists to receive left and right lane pixel indices left_lane_inds = [] right_lane_inds = [] # Step through the windows one by one for window in range(self.__nwindows): # Identify window boundaries in x and y (and right and left) win_y_low = binary_warped.shape[0] - (window + 1) * window_height win_y_high = binary_warped.shape[0] - window * window_height win_xleft_low = leftx_current - self.__margin win_xleft_high = leftx_current + self.__margin win_xright_low = rightx_current - self.__margin win_xright_high = rightx_current + self.__margin # Draw the windows on the visualization image cv2.rectangle(out_img, (win_xleft_low, win_y_low), (win_xleft_high, win_y_high), (0, 255, 0), 2) cv2.rectangle(out_img, (win_xright_low, win_y_low), (win_xright_high, win_y_high), (0, 255, 0), 2) good_left_inds = ((nonzeroy >= win_y_low) & (nonzeroy < win_y_high) & (nonzerox >= win_xleft_low) & (nonzerox < win_xleft_high)).nonzero()[0] good_right_inds = ((nonzeroy >= win_y_low) & (nonzeroy < win_y_high) & (nonzerox >= win_xright_low) & (nonzerox < win_xright_high)).nonzero()[0] # Append these indices to the lists left_lane_inds.append(good_left_inds) right_lane_inds.append(good_right_inds) if len(good_left_inds) > self.__minpixels: leftx_current = np.int(np.mean(nonzerox[good_left_inds])) if len(good_right_inds) > self.__minpixels: rightx_current = np.int(np.mean(nonzerox[good_right_inds])) # Concatenate the arrays of indices (previously was a list of lists of pixels) try: left_lane_inds = np.concatenate(left_lane_inds) right_lane_inds = np.concatenate(right_lane_inds) except ValueError: # Avoids an error if the above is not implemented fully pass # Extract left and right line pixel positions leftx = nonzerox[left_lane_inds] lefty = nonzeroy[left_lane_inds] rightx = nonzerox[right_lane_inds] righty = nonzeroy[right_lane_inds] return leftx, lefty, rightx, righty, out_img
# Variable for counting the no. of images count = 0 #checking existence of path assure_path_exists("training_data/") # Looping starts here while(True): # Capturing each video frame from the webcam _, image_frame = camera.read() # Converting each frame to grayscale image gray = cv2.cvtColor(image_frame, cv2.COLOR_BGR2GRAY) # Detecting different faces faces = face_detector.detectMultiScale(gray, 1.3, 5) # Looping through all the detected faces in the frame for (x,y,w,h) in faces: # Crop the image frame into rectangle cv2.rectangle(image_frame, (x,y), (x+w,y+h), (0,255,255), 2) # Increasing the no. of images by 1 since frame we captured count += 1 # Saving the captured image into the training_data folder cv2.imwrite("training_data/Person." + str(face_id) + '.' + str(count) + ".jpg", gray[y:y+h,x:x+w]) # Displaying the frame with rectangular bounded box cv2.imshow('frame', image_frame) # press 'q' for at least 100ms to stop this capturing process if cv2.waitKey(100) & 0xFF == ord('q'): break #We are taking 100 images for each person for the training data # If image taken reach 100, stop taking video elif count>100: break # Terminate video camera.release()
def compare_screen_get_information( self, path1, save_folder=save_folder_path, folder_csv="../CSV_ERROR", ssim=starts_ssim, image_format=starts_format_image ): """ Compare the already save image with the browser screen Compares the already saved image with the screen that is on the screen. If there is a difference, it saves the highlighted image to the: ../Outputs and making csv file with coordinates and elements which exist on this coordinates. Default folder for csv is ../CSV_ERROR path1 = path to the image to be compared to screen Example: Compare screen ../image1.png """ self._check_dir(save_folder) self._check_dir(folder_csv) self._check_ssim(ssim) self._check_image_format(image_format) save_folder = self.save_folder # Making screen self.seleniumlib.capture_page_screenshot(save_folder + "/test1.png") path2 = save_folder + "/test1.png" if os.path.exists(path1): if os.path.exists(path2): # load Img self._compare_images(path1, path2) # write coordinate with open(folder_csv + "/bug_coordinates.csv", "w") as csvfile: writer = csv.writer(csvfile) a = "path", "x_center", "y_center", "x", "y", "x1", "y1" writer.writerow(a) # Create frame in diff area for c in self.cnts: (x, y, w, h) = cv.boundingRect(c) cv.rectangle(self.img1, (x, y), (x + w, y + h), (0, 0, 255), 2) cv.rectangle(self.img2, (x, y), (x + w, y + h), (0, 0, 255), 2) x2 = x + w y2 = y + h x_center = x + ((x2 - x) / 2) y_center = y + ((y2 - y) / 2) f = path1, x_center, y_center, x, y, x2, y2 writer.writerow(f) # Save image and show report if float(self.score) < self.ssim: img_diff = cv.hconcat([self.img1, self.img2]) time_ = str(time.time()) self.seleniumlib.capture_page_screenshot( save_folder + "/Img{0}.{1}".format(time_, self.format) ) cv.imwrite(save_folder + "/Img{0}.{1}".format(time_, self.format), img_diff) # start reading coordinates and saving element from coordinate df = pd.read_csv(r"" + folder_csv + "/bug_coordinates.csv") with open( folder_csv + "/bug_co_and_name{0}.csv".format(str(time.time())), "w", ) as csv_name: writer = csv.writer(csv_name) a = "web-page", "x_center", "y_center", "class", "id", "name" writer.writerow(a) # Get information from position for i in range(len(df)): x_center = df.values[i, 1] y_center = df.values[i, 2] driver = self.seleniumlib.driver elements = driver.execute_script( "return document.elementsFromPoint(arguments[0], arguments[1]);", x_center, y_center, ) for element in elements: e_class = element.get_attribute("class") e_id = element.get_attribute("id") e_name = element.get_attribute("name") f = path1, x_center, y_center, e_class, e_id, e_name writer.writerow(f) score_percen = float(self.score) * 100 self.robotlib.fail("Image has diff: {} %".format(score_percen)) else: raise AssertionError("Bad or not exists path for picture or screen") else: raise AssertionError("Bad or not exists path for picture or screen")
def compare_screen_without_areas( self, path1, *args, save_folder=save_folder_path, ssim=starts_ssim, image_format=starts_format_image ): """ Compares two pictures, which have parts to be ignored x1 and y1 = x and y coordinates for the upper left corner of the ignored area square x2 and y2 = x and y coordinates for the lower right corner of the square of the ignored part Attention! It is always necessary to enter in order x1 y1 x2 y2 x1 y1 x2 y2 etc ... Compare screen without areas ../Image1.png 0 0 30 40 50 50 100 100 Creates 2 ignored parts at 0,0, 30,40 and 50, 50, 100, 100 """ self._check_dir(save_folder) self._check_ssim(ssim) self._check_image_format(image_format) save_folder = self.save_folder self.seleniumlib.capture_page_screenshot(save_folder + "/test1.png") path2 = save_folder + "/test1.png" if os.path.exists(path1) and os.path.exists(path2): lt = len(args) img1 = cv.imread(path1, 1) img2 = cv.imread(path2, 1) if lt % 4 == 0: x = lt / 4 self.robotlib.log_to_console(x) i = 0 a = 0 while i < x: color = (0, 0, 0) x1 = int(args[0 + a]) y1 = int(args[1 + a]) x2 = int(args[2 + a]) y2 = int(args[3 + a]) cv.rectangle(img1, (x1, y1), (x2, y2), color, -1) cv.rectangle(img2, (x1, y1), (x2, y2), color, -1) a += 4 i += 1 cv.namedWindow("image", cv.WINDOW_NORMAL) # convert to grey gray_img1 = cv.cvtColor(img1, cv.COLOR_BGR2GRAY) gray_img2 = cv.cvtColor(img2, cv.COLOR_BGR2GRAY) # SSIM diff Img (self.score, diff) = structural_similarity( gray_img1, gray_img2, full=True ) diff = (diff * 255).astype("uint8") # Threshold diff Img thresh = cv.threshold( diff, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU )[1] cnts = cv.findContours( thresh.copy(), cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE ) cnts = imutils.grab_contours(cnts) # Create frame in diff area for c in cnts: (x, y, w, h) = cv.boundingRect(c) cv.rectangle(img1, (x, y), (x + w, y + h), (0, 0, 255), 2) cv.rectangle(img2, (x, y), (x + w, y + h), (0, 0, 255), 2) # Show image if float(self.score) < self.ssim: img_diff = cv.hconcat([img1, img2]) time_ = str(time.time()) self.seleniumlib.capture_page_screenshot( save_folder + "/Img" + time_ + self.format ) cv.imwrite(save_folder + "/Img" + time_ + self.format, img_diff) self.robotlib.fail("Image has diff: {} ".format(self.score)) else: img_diff = cv.hconcat([img1, img2]) time_ = str(time.time()) self.seleniumlib.capture_page_screenshot( save_folder + "/Img" + time_ + self.format ) cv.imwrite(save_folder + "/Img" + time_ + self.format, img_diff) self.robotlib.log_to_console( "Image has diff: {} ".format(self.score) ) else: raise AssertionError("The path to the image does not exist")
import cv2 import numpy as np img = np.zeros((512, 512, 3), np.uint8) # img[:]=255,0,0 # OR [255,0,0] # print(img) cv2.line(img, (0, 0), (img.shape[1], img.shape[0]), (0, 255, 0), 3) #shape[1]=height, shape[0]=width cv2.rectangle(img, (0, 0), (250, 350), (0, 0, 255), cv2.FILLED) cv2.circle(img, (320, 200), 120, (255, 0, 0), 20) cv2.putText(img, "OPEN CV", (200, 400), cv2.FONT_HERSHEY_COMPLEX_SMALL, 3, (0, 150, 200), 4) cv2.imshow("Image", img) cv2.waitKey(0) """ Breaking/Splitting the image in 3 different channels (B,G,R) BY SHOWING THE IMAGE USING imshow IT WILL BE VISIBLE IN GRAYSCALE THEREFORE TO SHOW THE IMAGE COLOURFUL /BGR FORMAT ,ONLY ONE CHANNEL SHOULD BE DISPLAYED WITH OTHER 2 CHANNELS MADE ZERO """ # import numpy as np # import cv2 # # image = np.random.rand(200, 200, 3) # b, g, r = cv2.split(image) # cv2.imshow('green', g) # cv2.waitKey(0) # # black = np.zeros((200, 200, 3)) # black[:, :, 1] = g # Set only green channel
import cv2 import numpy as np import matplotlib.pyplot as plt cap = cv2.VideoCapture(0) img = cv2.imread('android.png') codec = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter('output.avi', codec, 20.0, (640, 480)) while True: ret, frame = cap.read() grayscale = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) cv2.line(grayscale, (50, 50), (150, 150), (0, 0, 0), 15) cv2.rectangle(grayscale, (50, 50), (200, 200), (0, 0, 0), 10) cv2.circle(grayscale, (100, 50), 55, (0, 0, 0), -1) cv2.putText(grayscale, 'rajat', (400, 400), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 5) rows, cols, channels = img.shape roi = frame[0:rows, 0:cols] img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) ret, mask = cv2.threshold(img_gray, 220, 255, cv2.THRESH_BINARY) mask_inv = cv2.bitwise_not(mask) frame_bg = cv2.bitwise_and(roi, roi, mask=mask) img_fg = cv2.bitwise_and(img, img, mask=mask_inv) add = frame_bg + img_fg frame[0:rows, 0:cols] = add
import numpy as np import cv2 face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml') cap = cv2.VideoCapture(0) while True: ret, img = cap.read() gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, 1.3, 5) for (x, y, w, h) in faces: cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2) roi_gray = gray[y:y + h, x:x + w] roi_color = img[y:y + h, x:x + w] eyes = eye_cascade.detectMultiScale(roi_gray) for (ex, ey, ew, eh) in eyes: cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2) cv2.imshow('img', img) k = cv2.waitKey(30) & 0xff if k == 27: break cap.release() cv2.destroyAllWindows()
print('pass1') im_pil = Image.fromarray(img) print('pass2') testImage = image.img_to_array(im_pil) testImage = np.expand_dims(testImage, axis=0) res = model.predict(testImage) if res[0][0] == 0: print("it is a human") count += 1 label = 'person' text = '{}: {:.0f}% c:{}'.format(label, confidence * 100, count) else: print("it is a mannequin") text = 'mannequin' + str(i) frame = cv2.rectangle(frame, first, second, color, 3) frame = cv2.putText(frame, text, first, cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 0), 2) cv2.imshow('frame', frame) resultK.write(frame) if cv2.waitKey(25) & 0xFF == ord('q'): cap.release() resultK.release() cv2.destroyAllWindows() break #print('count: ',count)
# of votes (note: in the event of an unlikely tie Python # will select first entry in the dictionary) name = max(counts, key=counts.get) # If someone in your dataset is identified, print their name on the screen if currentname != name: currentname = name print(currentname) # update the list of names names.append(name) # loop over the recognized faces for ((top, right, bottom, left), name) in zip(boxes, names): # draw the predicted face name on the image - color is in BGR cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2) y = top - 15 if top - 15 > 15 else top + 15 cv2.putText(frame, name, (left, y), cv2.FONT_HERSHEY_SIMPLEX, .8, (255, 0, 0), 2) # display the image to our screen cv2.imshow("Facial Recognition is Running", frame) key = cv2.waitKey(1) & 0xFF # quit when 'q' key is pressed if key == ord("q"): break # update the FPS counter fps.update()
def inference(image, conf_thresh=0.5, iou_thresh=0.4, target_shape=(160, 160), draw_result=True, show_result=True): ''' Main function of detection inference :param image: 3D numpy array of image :param conf_thresh: the min threshold of classification probabity. :param iou_thresh: the IOU threshold of NMS :param target_shape: the model input size. :param draw_result: whether to daw bounding box to the image. :param show_result: whether to display the image. :return: ''' # image = np.copy(image) output_info = [] height, width, _ = image.shape image_resized = cv2.resize(image, target_shape) image_np = image_resized / 255.0 # 归一化到0~1 image_exp = np.expand_dims(image_np, axis=0) y_bboxes_output, y_cls_output = tf_inference(sess, graph, image_exp) # remove the batch dimension, for batch is always 1 for inference. y_bboxes = decode_bbox(anchors_exp, y_bboxes_output)[0] y_cls = y_cls_output[0] # To speed up, do single class NMS, not multiple classes NMS. bbox_max_scores = np.max(y_cls, axis=1) bbox_max_score_classes = np.argmax(y_cls, axis=1) # keep_idx is the alive bounding box after nms. keep_idxs = single_class_non_max_suppression( y_bboxes, bbox_max_scores, conf_thresh=conf_thresh, iou_thresh=iou_thresh, ) for idx in keep_idxs: conf = float(bbox_max_scores[idx]) class_id = bbox_max_score_classes[idx] bbox = y_bboxes[idx] # clip the coordinate, avoid the value exceed the image boundary. xmin = max(0, int(bbox[0] * width)) ymin = max(0, int(bbox[1] * height)) xmax = min(int(bbox[2] * width), width) ymax = min(int(bbox[3] * height), height) if draw_result: if class_id == 0: color = (0, 255, 0) else: color = (255, 0, 0) cv2.rectangle(image, (xmin, ymin), (xmax, ymax), color, 2) cv2.putText(image, "%s: %.2f" % (id2class[class_id], conf), (xmin + 2, ymin - 2), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color) output_info.append([class_id, conf, xmin, ymin, xmax, ymax]) if show_result: Image.fromarray(image).show() return output_info
def detectFaceVideo(args): "Main function" # load our serialized model from disk print("[INFO] loading model...") net = cv2.dnn.readNetFromCaffe(ARGS["prototxt"], args["model"]) # initialize the video stream and allow the cammera sensor to warmup print("[INFO] starting video stream...") vid_stream = VideoStream(src=0).start() time.sleep(2.0) # loop over the frames from the video stream while True: # grab the frame from the threaded video stream and resize it # to have a maximum width of 400 pixels frame = vid_stream.read() frame = imutils.resize(frame, width=400) # grab the frame dimensions and convert it to a blob (height, width) = frame.shape[:2] blob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)), 1.0, (300, 300), (104.0, 177.0, 123.0)) # pass the blob through the network and obtain the detections and # predictions net.setInput(blob) detections = net.forward() # loop over the detections for i in range(0, detections.shape[2]): # extract the confidence (i.e., probability) associated with the # prediction confidence = detections[0, 0, i, 2] # filter out weak detections by ensuring the `confidence` is # greater than the minimum confidence if confidence < ARGS["confidence"]: continue # compute the (x, y)-coordinates of the bounding box for the # object box = detections[0, 0, i, 3:7] * np.array( [width, height, width, height]) (start_x, start_y, end_x, end_y) = box.astype("int") # draw the bounding box of the face along with the associated # probability text = "{:.2f}%".format(confidence * 100) _y = start_y - 10 if start_y - 10 > 10 else start_y + 10 cv2.rectangle(frame, (start_x, start_y), (end_x, end_y), (0, 0, 255), 2) cv2.putText(frame, text, (start_x, _y), cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2) # show the output frame cv2.imshow("Frame", frame) key = cv2.waitKey(1) & 0xFF # if the `q` key was pressed, break from the loop if key == ord("q"): break # do a bit of cleanup cv2.destroyAllWindows() vid_stream.stop()
frame = imutils.resize(frame, width = 800) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #print(frame.shape) faceRects = fd.detect(gray) for (x, y, w, h) in faceRects: roi = frame[y:y+h,x:x+w] roi = cv2.cvtColor(roi, cv2.COLOR_BGR2RGB) roi = cv2.resize(roi,(IMAGE_SIZE, IMAGE_SIZE)) min_dist = 1000 identity = "" detected = False for face in range(len(faces)): person = faces[face] dist, detected = verify(roi, person, database[person], FRmodel) if detected == True and dist<min_dist: min_dist = dist identity = person if detected == True: cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2) cv2.putText(frame, identity, (x+ (w//2),y-2), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 255), lineType=cv2.LINE_AA) cv2.imshow('frame', frame) out.write(frame) if cv2.waitKey(1) & 0xFF == ord('q'): break camera.release() out.release() cv2.destroyAllWindows()
def main(): try: src = sys.argv[1] except: src = 0 cap = video.create_capture(src) classifier_fn = 'digits_svm.dat' if not os.path.exists(classifier_fn): print '"%s" not found, run digits.py first' % classifier_fn return model = SVM() model.load(classifier_fn) while True: ret, frame = cap.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) bin = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 31, 10) bin = cv2.medianBlur(bin, 3) _, contours, heirs = cv2.findContours( bin.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE) try: heirs = heirs[0] except: heirs = [] for cnt, heir in zip(contours, heirs): _, _, _, outer_i = heir if outer_i >= 0: continue x, y, w, h = cv2.boundingRect(cnt) if not (16 <= h <= 64 and w <= 1.2*h): continue pad = max(h-w, 0) x, w = x-pad/2, w+pad cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0)) bin_roi = bin[y:,x:][:h,:w] gray_roi = gray[y:,x:][:h,:w] m = bin_roi != 0 if not 0.1 < m.mean() < 0.4: continue ''' v_in, v_out = gray_roi[m], gray_roi[~m] if v_out.std() > 10.0: continue s = "%f, %f" % (abs(v_in.mean() - v_out.mean()), v_out.std()) cv2.putText(frame, s, (x, y), cv2.FONT_HERSHEY_PLAIN, 1.0, (200, 0, 0), thickness = 1) ''' s = 1.5*float(h)/SZ m = cv2.moments(bin_roi) c1 = np.float32([m['m10'], m['m01']]) / m['m00'] c0 = np.float32([SZ/2, SZ/2]) t = c1 - s*c0 A = np.zeros((2, 3), np.float32) A[:,:2] = np.eye(2)*s A[:,2] = t bin_norm = cv2.warpAffine(bin_roi, A, (SZ, SZ), flags=cv2.WARP_INVERSE_MAP | cv2.INTER_LINEAR) bin_norm = deskew(bin_norm) if x+w+SZ < frame.shape[1] and y+SZ < frame.shape[0]: frame[y:,x+w:][:SZ, :SZ] = bin_norm[...,np.newaxis] sample = preprocess_hog([bin_norm]) digit = model.predict(sample)[0] cv2.putText(frame, '%d'%digit, (x, y), cv2.FONT_HERSHEY_PLAIN, 1.0, (200, 0, 0), thickness = 1) cv2.imshow('frame', frame) cv2.imshow('bin', bin) ch = cv2.waitKey(1) if ch == 27: break
def draw_bbox_anchors(tmp_img, xmin, ymin, xmax, ymax, color): anchor_dict = dragBBox.get_anchors_rectangles(xmin, ymin, xmax, ymax) for anchor_key in anchor_dict: x1, y1, x2, y2 = anchor_dict[anchor_key] cv2.rectangle(tmp_img, (int(x1), int(y1)), (int(x2), int(y2)), color, -1) return tmp_img
# -*- coding: utf-8 -*- """ Created on Mon Jun 15 16:44:05 2020 @author: Mateus Nobre Silva Almeida """ import cv2 listaimagens = ["imagem1.png","imagem2.jpg" ,"imagem3.png", "imagem4.png", "imagem5.png", "imagem6.png", "imagem7.png", "imagem8.png", "imagem9.png", "imagem10.png", "imagem11.png", "imagem12.png", "imagem13.jpg"] i = 0 while i < 13: image_path = 'imagensselecionar/' + listaimagens[i] # imagem cascade_path = 'haarcascade_frontalface_defalt.xml' # arquivo de cascade clf = cv2.CascadeClassifier(cascade_path) # cria o classificador img = cv2.imread(image_path) # ler a imagem img = cv2.resize(img, (900, 700)) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # converter para preto e branco / grayscale faces = clf.detectMultiScale(gray, 1.1, 10) for (x, y, w, h) in faces: img = cv2.rectangle(img, (x, y), (x + w, y + h), (255, 255, 0), 2) # desenha um rec cv2.imshow('image', img) cv2.waitKey(0) cv2.destroyAllWindows() if(i == 11): i = 0 else: i = i + 1
_, pt_1, pt_2 = get_face(img_rgb, res['box']) else: face, pt_1, pt_2 = get_face(img_rgb, res['box']) encode = get_encode(face_encoder, face, required_size) encode = l2_normalizer.transform(np.expand_dims(encode, axis=0))[0] name = 'unknown' distance = float("inf") for key, val in encoding_dict.items(): dist = cosine(val, encode) if dist < recognition_t and dist < distance: name = key distance = dist if name == 'unknown': cv2.rectangle(img, pt_1, pt_2, (0, 0, 255), 2) cv2.putText(img, name, pt_1, cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255), 2) else: cv2.rectangle(img, pt_1, pt_2, (0, 255, 0), 2) cv2.putText(img, name + f'__{distance:.2f}', pt_1, cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2) if do_align: cv2.putText(img, 'With Alignment', (200, 75), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 3) else: cv2.putText(img, 'No Alignment', (200, 75), cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255), 3) cv2.imwrite(test_res_path, img) plt_show(img)
# "/home/alex/Desktop/projects/minimal-object-detector/src/train/data/images/2020-Toyota-86-GT-TRD-Wheels.jpg" # ) for i in range(num_data): filename = testdata_path + str(i) + ".jpg" print("test filename", filename) image = cv2.imread(filename) image_ori = cv2.resize(image, (512, 512)) image = normalize(image_ori) model = detector.Detector(timestamp="2021-04-22T11.25.25") model.eval() with torch.no_grad(): image = torch.Tensor(image) if torch.cuda.is_available(): image = image.cuda() boxes = model.get_boxes(image.permute(2, 0, 1).unsqueeze(0)) for box in boxes[0]: # print(box) # print(box.confidence) confidence = float(box.confidence) box = (box.box * torch.Tensor([512] * 4)).int().tolist() if confidence > 0.35: # print(box) cv2.rectangle(image_ori, (box[0], box[1]), (box[2], box[3]), (255, 0, 0), 2) savefilename = save_path + str(i) + ".jpg" cv2.imwrite(savefilename, image_ori)
#print(file_list[I]) file_list[I] = (np.unique(file_list[I])).tolist() #print(file_list[I]) ''' results = [] for k in range(ret): idx = int(labels[order[k]]) #idx = file_list[I-1][idx%len(file_list[I-1])] #print(idx) img = cv2.imread(origindataset.data[idx%20939].replace('simline', 'img').replace('.png', '.jpg')) img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) x1,y1,x2,y2 = rois_ns[order[k]] cropped=img[max(0,y1):min(1170,y2), max(0,x1):min(827,x2),:] results.append(img[y1:y2, x1:x2,:]) cv2.rectangle(img, (x1,y1), (x2,y2),(0,255,0), 3) if simline: cv2.imwrite('%s/Snormalize%d%s_%d.png'%(args.outf, j, tdataset.data[j].split('/')[-1],k), img) else: cv2.imwrite('%s/normalize%d%s_%d.png'%(args.outf, j, tdataset.data[j].split('/')[-1],k), img) fig = plt.figure(figsize=(13, 18)) columns = 5 rows = 8 # ax enables access to manipulate each of subplots ax = [] for i in range(len(results)): img = results[i] sim = nscores[i] # create subplot and append to ax t = fig.add_subplot(rows, columns, i+1)
def face_predict(image,model,class_list,classes): ##############顔の特徴を抽出する################### # グレースケール処理 img_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) #検出器を使用して画像内の顔を認識し、顔リストを作成します face_dets = detector(img_gray, 1) #顔がない場合は、画像を元のパスに戻します if len(face_dets) == 0: return image else: for det in face_dets: #顔領域を抽出 face_top = det.top() if det.top() > 0 else 0 face_bottom = det.bottom() if det.bottom() > 0 else 0 face_left = det.left() if det.left() > 0 else 0 face_right = det.right() if det.right() > 0 else 0 face_temp = img_gray[face_top:face_bottom, face_left:face_right] #灰度图 face_img = None #圧縮画像は64 * 64 little_face = reszie_image(face_temp) #################################################### # ディメンションの順序は、バックエンドシステムに従って決定されます if K.image_data_format() == 'channels_first': face_img = little_face.reshape((1,1,img_size, img_size)) #与模型训练不同,这次只是针对1张图片进行预测 elif K.image_data_format() == 'channels_last': face_img = little_face.reshape((1, img_size, img_size, 1)) #浮動小数点と正規化 face_img = face_img.astype('float32') face_img /= 255 #入力が各カテゴリに属する確率を与える result_probability = model.predict_proba(face_img) #print('result:', result_probability) # カテゴリ予測を与える(変更) if max(result_probability[0]) >= 0.9: result = model.predict_classes(face_img) #print('result:', result) #カテゴリ予測結果を返す faceID = result[0] else: faceID = -1 #額縁 cv2.rectangle(image, (face_left - 10, face_top - 10), (face_right + 10, face_bottom + 10), color, thickness=2) #face_id判定 if faceID in classes: # テキストプロンプトは誰ですか cv2.putText(image, class_list[faceID], (face_left, face_top - 30), # 座標 cv2.FONT_HERSHEY_SIMPLEX, # フォント 1, # フォントサイズ (255, 0, 255), # 色 2) # ワードライン幅 else: # テキストプロンプトは誰ですか cv2.putText(image, 'None ', (face_left, face_top - 30), # 座標 cv2.FONT_HERSHEY_SIMPLEX, # フォント 1, # フォントサイズ (255, 0, 255), # 色 2) # ワードライン幅 return image
import re pattern = re.compile("non") newx = [] newx2 = [] for i in range(len(cols) - 1): if pattern.match(cols[i]): continue else: if cols[i] == cols[i + 1]: newx.append(cols[i]) else: newx.append('no') #print(newx) for i in range(len(newx)): if newx[i] == 'no': if newx[i - 1] != 'no': newx2.append(newx[i - 1]) print(newx2) value = resistance(newx2) print(value, 'ohms') cv2.line(img, (pty, ptx), (ptymax, ptxmax), (255, 0, 0), 5) cv2.rectangle(img, (y, x), (y1, x1), (0, 0, 255), 3) cv2.imshow('image', img) cv2.waitKey(0) cv2.destroyAllWindows()
def qr_decode_pyzbar(image, count=None, nofile=True): """ Search for QRcode in image. Args: image: input image. Probably ndarray count: int count for cycle function called from nofile: bool Return image with boxes or not. Returns: count: count if count is not None. Else 0. image: ndarray if nofile=True. Else 0. detected_barcodes: deque deque with detected barcodes barcode_pos: deque(list) for each element there is list which contains (x, y, w, h) where w, h is width and height """ # gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # gray_image = cv2.resize(gray_image, (640, 360)) barcodes = pyzbar.decode(image) detected_barcodes = deque() barcodes_areas = deque() barcode_pos = deque() (x, y, w, h) = 0, 0, 0, 0 # loop over the detected barcodes for barcode in barcodes: # extract the bounding box location of the barcode and draw the # bounding box surrounding the barcode on the image (x, y, w, h) = barcode.rect # Probably pass list of coordinates of qr barcode_pos.append((x, y, w, h)) barcodes_areas.append(w*h) # the barcode data is a bytes object so if we want to draw it on # our output image we need to convert it to a string first barcodeData = barcode.data.decode("utf-8") barcodeType = barcode.type # draw the barcode data and barcode type on the image if nofile is False: cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 2) text = "{} ({})".format(barcodeData, barcodeType) cv2.putText(image, text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) detected_barcodes.append(barcodeData) return_list = [] if count is None: return_list.append(0) else: return_list.append(count) if nofile: return_list.append(0) else: return_list.append(image) return_list.append(detected_barcodes) return_list.append(barcode_pos) return return_list
def analysis(db_path, model_name, distance_metric, enable_face_analysis = True): input_shape = (224, 224) text_color = (255,255,255) employees = [] #check passed db folder exists if os.path.isdir(db_path) == True: for r, d, f in os.walk(db_path): # r=root, d=directories, f = files for file in f: if ('.jpg' in file): #exact_path = os.path.join(r, file) exact_path = r + "/" + file #print(exact_path) employees.append(exact_path) #------------------------ if len(employees) > 0: if model_name == 'VGG-Face': print("Using VGG-Face model backend and", distance_metric,"distance.") model = VGGFace.loadModel() input_shape = (224, 224) elif model_name == 'OpenFace': print("Using OpenFace model backend", distance_metric,"distance.") model = OpenFace.loadModel() input_shape = (96, 96) elif model_name == 'Facenet': print("Using Facenet model backend", distance_metric,"distance.") model = Facenet.loadModel() input_shape = (160, 160) elif model_name == 'DeepFace': print("Using FB DeepFace model backend", distance_metric,"distance.") model = FbDeepFace.loadModel() input_shape = (152, 152) else: raise ValueError("Invalid model_name passed - ", model_name) #------------------------ #tuned thresholds for model and metric pair threshold = functions.findThreshold(model_name, distance_metric) #------------------------ #facial attribute analysis models if enable_face_analysis == True: tic = time.time() emotion_model = Emotion.loadModel() print("Emotion model loaded") #age_model = Age.loadModel() #print("Age model loaded") #gender_model = Gender.loadModel() #print("Gender model loaded") toc = time.time() print("Facial attibute analysis models loaded in ",toc-tic," seconds") #------------------------ #find embeddings for employee list tic = time.time() pbar = tqdm(range(0, len(employees)), desc='Finding embeddings') embeddings = [] #for employee in employees: for index in pbar: employee = employees[index] pbar.set_description("Finding embedding for %s" % (employee.split("/")[-1])) embedding = [] img = functions.detectFace(employee, input_shape) img_representation = model.predict(img)[0,:] embedding.append(employee) embedding.append(img_representation) embeddings.append(embedding) df = pd.DataFrame(embeddings, columns = ['employee', 'embedding']) df['distance_metric'] = distance_metric toc = time.time() print("Embeddings found for given data set in ", toc-tic," seconds") #----------------------- evaluation_rounds = 3 round = 0 guesses = [] time_threshold = 0.1; frame_threshold = 0 pivot_img_size = 112 #face recognition result image #----------------------- opencv_path = functions.get_opencv_path() face_detector_path = opencv_path+"haarcascade_frontalface_default.xml" face_cascade = cv2.CascadeClassifier(face_detector_path) #----------------------- freeze = False face_detected = False face_included_frames = 0 #freeze screen if face detected sequantially 5 frames freezed_frame = 0 tic = time.time() ##### loop de captura e analise da imagem ####################################################################### print("------------------------------------------------") print("- Modulo de reconhecimento facial iniciado... -") print("------------------------------------------------") # criando o server socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((HOST, PORT)) s.listen() while(True): resultado = "indefinido" cap = cv2.VideoCapture(0) #webcam print("Aguardando a conexao com o EVA...") conn, addr = s.accept() # funcao (block) aguarda conexao print("Ligando a WebCam") for i in range(10): # numero de leituras necessarias ###############print("valor de i:", i) ret, img = cap.read() #cv2.namedWindow('img', cv2.WINDOW_FREERATIO) #cv2.setWindowProperty('img', cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN) raw_img = img.copy() resolution = img.shape resolution_x = img.shape[1]; resolution_y = img.shape[0] if freeze == False: faces = face_cascade.detectMultiScale(img, 1.3, 5) if len(faces) == 0: face_included_frames = 0 else: faces = [] detected_faces = [] face_index = 0 for (x,y,w,h) in faces: if w > 130: #discard small detected faces face_detected = True if face_index == 0: face_included_frames = face_included_frames + 1 #increase frame for a single face cv2.rectangle(img, (x,y), (x+w,y+h), (67,67,67), 1) #draw rectangle to main image cv2.putText(img, str(frame_threshold - face_included_frames), (int(x+w/4),int(y+h/1.5)), cv2.FONT_HERSHEY_SIMPLEX, 4, (255, 255, 255), 2) detected_face = img[int(y):int(y+h), int(x):int(x+w)] #crop detected face #------------------------------------- detected_faces.append((x,y,w,h)) face_index = face_index + 1 #------------------------------------- # if face_detected == True and face_included_frames == frame_threshold and freeze == False: if face_detected == True and freeze == False: round += 1 freeze = True #base_img = img.copy() base_img = raw_img.copy() detected_faces_final = detected_faces.copy() tic = time.time() if freeze == True: toc = time.time() if (toc - tic) < time_threshold: if freezed_frame == 0: freeze_img = base_img.copy() #freeze_img = np.zeros(resolution, np.uint8) #here, np.uint8 handles showing white area issue for detected_face in detected_faces_final: x = detected_face[0]; y = detected_face[1] w = detected_face[2]; h = detected_face[3] #cv2.rectangle(freeze_img, (x,y), (x+w,y+h), (67,67,67), 1) #draw rectangle to main image #------------------------------- #apply deep learning for custom_face custom_face = base_img[y:y+h, x:x+w] #------------------------------- #facial attribute analysis if enable_face_analysis == True: gray_img = functions.detectFace(custom_face, (48, 48), True) #emotion_labels = ['Angry', 'Disgust', 'Fear', 'Happy', 'Sad', 'Surprise', 'Neutral'] emotion_labels = ['raiva', 'desgostoso', 'medo', 'feliz', 'triste', 'surpreso', 'neutro'] emotion_predictions = emotion_model.predict(gray_img)[0,:] sum_of_predictions = emotion_predictions.sum() mood_items = [] for i in range(0, len(emotion_labels)): mood_item = [] emotion_label = emotion_labels[i] emotion_prediction = 100 * emotion_predictions[i] / sum_of_predictions mood_item.append(emotion_label) mood_item.append(emotion_prediction) mood_items.append(mood_item) emotion_df = pd.DataFrame(mood_items, columns = ["emotion", "score"]) emotion_df = emotion_df.sort_values(by = ["score"], ascending=False).reset_index(drop=True) #print(emotion_df) #print(mood_items) # mood = dict() # for item in mood_items: # mood[item[0]] = item[1] if emotion_df["score"][0] < 33: round -= 1 else: guesses.append(emotion_df["emotion"][0]) if round == evaluation_rounds: guesses_score = dict() for guess in guesses: guesses_score[guess] = 0 for guess in guesses: guesses_score[guess] += 1 ordered_guesses_score = {k: v for k, v in sorted(guesses_score.items(), key=lambda item: item[1], reverse=True)} resultado = next(iter(ordered_guesses_score)) #conn.sendall(resultado.encode()) # envia a expressao (codificada em bytes) para o cliente #print("Expressao detectada: " + resultado) #print(ordered_guesses_score) ######################################### mqtt #client.publish("topic/emotion_recog", resultado) round = 0 guesses = [] #background of mood box #transparency overlay = freeze_img.copy() opacity = 0.4 ###################################### """if x+w+pivot_img_size < resolution_x: #right #cv2.rectangle(freeze_img #, (x+w,y+20) # , (x+w,y) # , (x+w+pivot_img_size, y+h) # , (64,64,64),cv2.FILLED) # #cv2.addWeighted(overlay, opacity, freeze_img, 1 - opacity, 0, freeze_img) # #elif x-pivot_img_size > 0: #left # cv2.rectangle(freeze_img #, (x-pivot_img_size,y+20) # , (x-pivot_img_size,y) # , (x, y+h) # , (64,64,64),cv2.FILLED) # # cv2.addWeighted(overlay, opacity, freeze_img, 1 - opacity, 0, freeze_img) for index, instance in emotion_df.iterrows(): emotion_label = "%s " % (instance['emotion']) emotion_score = instance['score']/100 bar_x = 35 #this is the size if an emotion is 100% bar_x = int(bar_x * emotion_score) if x+w+pivot_img_size < resolution_x: text_location_y = y + 20 + (index+1) * 20 text_location_x = x+w if text_location_y < y + h: cv2.putText(freeze_img, emotion_label, (text_location_x, text_location_y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1) cv2.rectangle(freeze_img , (x+w+70, y + 13 + (index+1) * 20) , (x+w+70+bar_x, y + 13 + (index+1) * 20 + 5) , (255,255,255), cv2.FILLED) elif x-pivot_img_size > 0: text_location_y = y + 20 + (index+1) * 20 text_location_x = x-pivot_img_size if text_location_y <= y+h: cv2.putText(freeze_img, emotion_label, (text_location_x, text_location_y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1) cv2.rectangle(freeze_img , (x-pivot_img_size+70, y + 13 + (index+1) * 20) , (x-pivot_img_size+70+bar_x, y + 13 + (index+1) * 20 + 5) , (255,255,255), cv2.FILLED) #------------------------------- """ face_224 = functions.detectFace(custom_face, (224, 224), False) """ age_predictions = age_model.predict(face_224)[0,:] apparent_age = Age.findApparentAge(age_predictions) #------------------------------- gender_prediction = gender_model.predict(face_224)[0,:] if np.argmax(gender_prediction) == 0: gender = "W" elif np.argmax(gender_prediction) == 1: gender = "M" #print(str(int(apparent_age))," years old ", dominant_emotion, " ", gender) analysis_report = str(int(apparent_age))+" "+gender """ #------------------------------- ############################### """info_box_color = (46,200,255) #top if y - pivot_img_size + int(pivot_img_size/5) > 0: triangle_coordinates = np.array( [ (x+int(w/2), y) , (x+int(w/2)-int(w/10), y-int(pivot_img_size/3)) , (x+int(w/2)+int(w/10), y-int(pivot_img_size/3)) ] ) cv2.drawContours(freeze_img, [triangle_coordinates], 0, info_box_color, -1) cv2.rectangle(freeze_img, (x+int(w/5), y-pivot_img_size+int(pivot_img_size/5)), (x+w-int(w/5), y-int(pivot_img_size/3)), info_box_color, cv2.FILLED) # cv2.putText(freeze_img, analysis_report, (x+int(w/3.5), y - int(pivot_img_size/2.1)), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 111, 255), 2) #bottom elif y + h + pivot_img_size - int(pivot_img_size/5) < resolution_y: triangle_coordinates = np.array( [ (x+int(w/2), y+h) , (x+int(w/2)-int(w/10), y+h+int(pivot_img_size/3)) , (x+int(w/2)+int(w/10), y+h+int(pivot_img_size/3)) ] ) cv2.drawContours(freeze_img, [triangle_coordinates], 0, info_box_color, -1) cv2.rectangle(freeze_img, (x+int(w/5), y + h + int(pivot_img_size/3)), (x+w-int(w/5), y+h+pivot_img_size-int(pivot_img_size/5)), info_box_color, cv2.FILLED) # cv2.putText(freeze_img, analysis_report, (x+int(w/3.5), y + h + int(pivot_img_size/1.5)), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 111, 255), 2) """ #------------------------------- #face recognition custom_face = functions.detectFace(custom_face, input_shape) #check detectFace function handled if custom_face.shape[1:3] == input_shape: if df.shape[0] > 0: #if there are images to verify, apply face recognition img1_representation = model.predict(custom_face)[0,:] #print(freezed_frame," - ",img1_representation[0:5]) def findDistance(row): distance_metric = row['distance_metric'] img2_representation = row['embedding'] distance = 1000 #initialize very large value if distance_metric == 'cosine': distance = dst.findCosineDistance(img1_representation, img2_representation) elif distance_metric == 'euclidean': distance = dst.findEuclideanDistance(img1_representation, img2_representation) elif distance_metric == 'euclidean_l2': distance = dst.findEuclideanDistance(dst.l2_normalize(img1_representation), dst.l2_normalize(img2_representation)) return distance df['distance'] = df.apply(findDistance, axis = 1) df = df.sort_values(by = ["distance"]) candidate = df.iloc[0] employee_name = candidate['employee'] best_distance = candidate['distance'] if best_distance <= threshold: #print(employee_name) display_img = cv2.imread(employee_name) display_img = cv2.resize(display_img, (pivot_img_size, pivot_img_size)) label = employee_name.split("/")[-1].replace(".jpg", "") label = re.sub('[0-9]', '', label) ########################################## """try: if y - pivot_img_size > 0 and x + w + pivot_img_size < resolution_x: #top right freeze_img[y - pivot_img_size:y, x+w:x+w+pivot_img_size] = display_img overlay = freeze_img.copy(); opacity = 0.4 cv2.rectangle(freeze_img,(x+w,y),(x+w+pivot_img_size, y+20),(46,200,255),cv2.FILLED) cv2.addWeighted(overlay, opacity, freeze_img, 1 - opacity, 0, freeze_img) cv2.putText(freeze_img, label, (x+w, y+10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, text_color, 1) #connect face and text cv2.line(freeze_img,(x+int(w/2), y), (x+3*int(w/4), y-int(pivot_img_size/2)),(67,67,67),1) cv2.line(freeze_img, (x+3*int(w/4), y-int(pivot_img_size/2)), (x+w, y - int(pivot_img_size/2)), (67,67,67),1) elif y + h + pivot_img_size < resolution_y and x - pivot_img_size > 0: #bottom left freeze_img[y+h:y+h+pivot_img_size, x-pivot_img_size:x] = display_img overlay = freeze_img.copy(); opacity = 0.4 cv2.rectangle(freeze_img,(x-pivot_img_size,y+h-20),(x, y+h),(46,200,255),cv2.FILLED) cv2.addWeighted(overlay, opacity, freeze_img, 1 - opacity, 0, freeze_img) cv2.putText(freeze_img, label, (x - pivot_img_size, y+h-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, text_color, 1) #connect face and text cv2.line(freeze_img,(x+int(w/2), y+h), (x+int(w/2)-int(w/4), y+h+int(pivot_img_size/2)),(67,67,67),1) cv2.line(freeze_img, (x+int(w/2)-int(w/4), y+h+int(pivot_img_size/2)), (x, y+h+int(pivot_img_size/2)), (67,67,67),1) elif y - pivot_img_size > 0 and x - pivot_img_size > 0: #top left freeze_img[y-pivot_img_size:y, x-pivot_img_size:x] = display_img overlay = freeze_img.copy(); opacity = 0.4 cv2.rectangle(freeze_img,(x- pivot_img_size,y),(x, y+20),(46,200,255),cv2.FILLED) cv2.addWeighted(overlay, opacity, freeze_img, 1 - opacity, 0, freeze_img) cv2.putText(freeze_img, label, (x - pivot_img_size, y+10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, text_color, 1) #connect face and text cv2.line(freeze_img,(x+int(w/2), y), (x+int(w/2)-int(w/4), y-int(pivot_img_size/2)),(67,67,67),1) cv2.line(freeze_img, (x+int(w/2)-int(w/4), y-int(pivot_img_size/2)), (x, y - int(pivot_img_size/2)), (67,67,67),1) elif x+w+pivot_img_size < resolution_x and y + h + pivot_img_size < resolution_y: #bottom righ freeze_img[y+h:y+h+pivot_img_size, x+w:x+w+pivot_img_size] = display_img overlay = freeze_img.copy(); opacity = 0.4 cv2.rectangle(freeze_img,(x+w,y+h-20),(x+w+pivot_img_size, y+h),(46,200,255),cv2.FILLED) cv2.addWeighted(overlay, opacity, freeze_img, 1 - opacity, 0, freeze_img) cv2.putText(freeze_img, label, (x+w, y+h-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, text_color, 1) #connect face and text cv2.line(freeze_img,(x+int(w/2), y+h), (x+int(w/2)+int(w/4), y+h+int(pivot_img_size/2)),(67,67,67),1) cv2.line(freeze_img, (x+int(w/2)+int(w/4), y+h+int(pivot_img_size/2)), (x+w, y+h+int(pivot_img_size/2)), (67,67,67),1) except Exception as err: print(str(err)) """ tic = time.time() #in this way, freezed image can show 5 seconds #------------------------------- time_left = int(time_threshold - (toc - tic) + 1) #cv2.rectangle(freeze_img, (10, 10), (90, 50), (67,67,67), -10) #cv2.putText(freeze_img, str(time_left), (40, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 1) #cv2.imshow('img', freeze_img) freezed_frame = freezed_frame + 1 else: face_detected = False face_included_frames = 0 freeze = False freezed_frame = 0 print("Expressao detectada: " + resultado) conn.sendall(resultado.encode()) # envia a expressao (codificada em bytes) para o cliente print("Desligando a Webcam...") cap.release() print("Fim da conexao") print("------------------------------------------------") conn.close() #else: #cv2.imshow('img',img) #if cv2.waitKey(1) & 0xFF == ord('q'): #press q to quit # break # fim do while #kill open cv things cap.release() cv2.destroyAllWindows()
frame_height, frame_width, _ = frame.shape model.setInput(cv2.dnn.blobFromImage(frame, size=(300, 300), swapRB=True)) output = model.forward() for detection in output[0, 0, :, :]: confidence = detection[2] if confidence > .5: class_id = detection[1] class_name = id_class_name(class_id,classNames) # print(str(str(class_id) + " " + str(detection[2]) + " " + class_name)) box_x = detection[3] * frame_width box_y = detection[4] * frame_height box_width = detection[5] * frame_width box_height = detection[6] * frame_height cv2.rectangle(frame, (int(box_x), int(box_y)), (int(box_width), int(box_height)), (23, 230, 210), thickness=1) cv2.putText(frame, class_name, (int(box_x), int(box_y + .05 * frame_height)), cv2.FONT_HERSHEY_SIMPLEX, (.005 * frame_width), (0, 0, 255)) if cv2.waitKey(1) & 0xFF == ord('s'): # object_name = class_name center_X.append(int((box_x + box_width) / 2)) center_Y.append(int((box_y + box_height) / 2)) print(class_name, center_X[-1], center_Y[-1]) if len(center_X) != 0: cv2.circle(frame, (center_X[-1], center_Y[-1]), 2, (0, 0, 0), -1) cv2.waitKey(1) cv2.imshow('Detection', frame)
cv2.THRESH_BINARY)[1] thresh = cv2.dilate(thresh, None, iterations=2) cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts) # loop over the contours for c in cnts: # if the contour is too small, ignore it if cv2.contourArea(c) < conf["min_area"]: continue # compute the bounding box for the contour, draw it on the frame, # and update the text (x, y, w, h) = cv2.boundingRect(c) cv2.rectangle(gray_fresh, (x, y), (x + w, y + h), (0xff), 2) text = "Occupied" # draw the text and timestamp on the frame ts = timestamp.strftime("%A %d %B %Y %I:%M:%S%p") cv2.putText(gray_fresh, "Room Status: {}".format(text), (10, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0xff), 2) cv2.putText(gray_fresh, ts, (10, gray_fresh.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0xff), 1) # check to see if the room is occupied if text == "Occupied": # check to see if enough time has passed between uploads if (timestamp - lastUploaded).seconds >= conf["min_upload_seconds"]: # increment the motion counter motionCounter += 1
green = (0, 255, 0) for image in humans: gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) rectangles = detector(gray) rectCount = 0 for rect in rectangles: rectCount += 1 if rectCount == 1: topLeft = (int(rectangles[0].tl_corner().x), int( rectangles[0].tl_corner().y)) bottomRight = (int(rectangles[0].br_corner().x), int(rectangles[0].br_corner().y)) image_withRect = image.copy() image_withRect = cv2.rectangle(image_withRect, topLeft, bottomRight , color = green , thickness = 2) cv2_imshow(image_withRect) points = predictor(gray, rectangles[0]) point_np = np.zeros_like(animalPoints[0]) for i in range(68): point_np[i] = [points.part(i).x, points.part(i).y] humanPoints.append(point_np) else: print("Wrong Rectangle Count") animalsPointed = [] for image, points in zip(animals, animalPoints): image_withPoints = image.copy() for i in range(68):
#load the cascade face_classifier = cv2.CascadeClassifier('haarcascade_frontface.xml') smile_classifier = cv2.CascadeClassifier('haarcascade_smile.xml') times = [] smile_ratios = [] #read the image from the video cap = cv2.VideoCapture(0) while 1: ret, img = cap.read() gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) gray = cv2.GaussianBlur(gray, (21, 21), 0) faces = face_classifier.detectMultiScale(gray, 1.3, 5) for (x, y, w, h) in faces: cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 1) roi_gray = gray[y:y + h, x:x + w] roi_img = img[y:y + h, x:x + w] smile = smile_classifier.detectMultiScale(roi_gray, scaleFactor=1.2, minNeighbors=22, minSize=(25, 25)) for (sx, sy, sw, sh) in smile: cv2.rectangle(roi_img, (sx, sy), (sx + sw, sy + sh), (0, 255, 0), 1) sm_ratio = str(round(sw / sh, 3)) font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText(img, 'Smile meter : ' + sm_ratio, (10, 50), font, 1, (200, 255, 155), 2, cv2.LINE_AA) if float(sm_ratio) > 1.8: smile_ratios.append(float(sm_ratio))
while True: # 1) capture a new frame from the camera t1 = cv2.getTickCount() ret, frame = cap.read() faces = face_cascade.detectMultiScale(frame, 1.3, 5) top = [] left = [] # if there is face in frame if type(faces) is np.ndarray: face = np.zeros((faces.shape[0], 96, 96)) person = 0 for (x, y, w, h) in faces: left.append(x) top.append(y) cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 255, 255), 2) if w > h: s = w else: s = h crop = frame[y:y+s, x:x+s] gray = cv2.cvtColor(crop, cv2.COLOR_BGR2GRAY) face[person] = cv2.resize(gray, (96, 96)) person += 1 # reshape face numpy into the shape of trained data face = face[:, :, :, np.newaxis] predictable = False # add frame of everyone in their sequence