def countcotours(img): nc=0 dir="img/"+img+".png" image = cv2.imread(dir) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) cv2.imshow("Image", gray) cv2.waitKey(0) blurred = cv2.GaussianBlur(gray, (5, 5), 0) cv2.imshow("Image", blurred) cv2.waitKey(0) thresh = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY)[1] cv2.imshow("Image", thresh) cv2.waitKey(0) cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[0] if imutils.is_cv2() else cnts[1] sd = ShapeDetector() for c in cnts: # compute the center of the contour if cv2.contourArea(c)<800: continue else: M = cv2.moments(c) cX = int(M["m10"] / M["m00"]) cY = int(M["m01"] / M["m00"]) shape = sd.detect(c) #if(shape=="square" or shape=="rectangle"): if 1==1: cv2.drawContours(image, [c], -1, (0, 255, 0), 2) cv2.circle(image, (cX, cY), 7, (255, 255, 255), -1) cv2.putText(image, "center", (cX - 20, cY - 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2) nc=nc+1 cv2.imshow("Image", image) cv2.waitKey(0) return nc
def findcolors(nc,im): cont=0 image = cv2.imread(im) blurred = cv2.GaussianBlur(image, (5, 5), 0) gray = cv2.cvtColor(blurred, cv2.COLOR_BGR2GRAY) lab = cv2.cvtColor(blurred, cv2.COLOR_BGR2LAB) thresh = cv2.threshold(gray, 60, 255, cv2.THRESH_BINARY)[1] cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[0] if imutils.is_cv2() else cnts[1] cl = ColorLabeler() sd = ShapeDetector() for c in cnts: if cv2.contourArea(c)< 800: continue else: M = cv2.moments(c) cX = int((M["m10"] / M["m00"])) cY = int((M["m01"] / M["m00"])) shape = sd.detect(c) color = cl.label(lab, c) print(shape) print(color) if (shape=="square" or shape=="rectangle"): if (color==nc): text = "{}".format(color) cv2.drawContours(image, [c], -1, (0, 255, 0), 2) cv2.putText(image, text, (cX, cY), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2) cont=cont+1 cv2.imshow("Image", image) cv2.waitKey(0) return cont
def main(): sd = ShapeDetector() # Constallation Object cd = ConstellationBuilder() file = sys.argv[1] constellations = cd.build_all() for constellation in constellations: fig, ax = plt.subplots() plt.scatter(constellation.stars_x, constellation.stars_y) # plt.axis([0, 8, -5, 0]) for i, txt in enumerate(constellation.stars_mags): ax.annotate(txt, (constellation.stars_x[i], constellation.stars_y[i])) for line in constellation.lines: plt.plot((line.item(0), line.item(2)), (-line.item(1), -line.item(3)), 'ro-', linewidth=2, markersize=0) plt.show() # Read in Image img = cv2.imread(file, cv2.IMREAD_COLOR) final_img = cv2.imread(file, cv2.IMREAD_COLOR) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # cv2.imshow('Starting Image', img) # cv2.waitKey(0) # cv2.destroyAllWindows() #Threshold to binary thresh = 100 img = cv2.threshold(img, thresh, 255, cv2.THRESH_BINARY)[1] stars = sd.get_all(img) x, y, mags = sd.detect_stars(stars) fig, ax = plt.subplots() plt.scatter(x, y) for i, txt in enumerate(mags): ax.annotate(2 * txt, (x[i], y[i])) #Find two brightest stars in image and mark them cd = ConstellationDetector(constellations) sorted_mags = order_mags(mags) l1, l2 = sorted_mags[0], sorted_mags[1] x_test = x.copy() y_test = y.copy() mags_test = mags.copy() for j in range(0, len(mags)): for constellation in constellations: for i in range(j, len(mags) - 1): tx, ty, lines, t_scale, matched = cd.search_for_constellation( constellation, x_test, y_test, mags_test, sorted_mags[j], sorted_mags[i]) if matched: break if matched: break if matched: break plt.plot(x_test[l1], y_test[l1], 'r+') plt.plot(x_test[l2], y_test[l2], 'y+') if matched: plt.plot(tx, ty, 'y*') for line in lines: plt.plot((line.item(0), line.item(2)), (-line.item(1), -line.item(3)), 'ro-', linewidth=2, markersize=0) # Draw constellation on image draw_stars(tx, ty, t_scale, final_img) draw_lines(lines, final_img) plt.show() height, width, channels = img.shape final_img = cv2.resize(final_img, (width * 2, height * 2)) cv2.imshow('Finished Product', final_img) cv2.waitKey(0) cv2.destroyAllWindows()
except: pass thresh = cv2.threshold(blurred, 0, 255, cv2.THRESH_BINARY)[1] # find contours in the thresholded image and initialize the # shape detector cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) totalArea = 0 try: totalArea = cv2.contourArea(cnts[1][0]) except: pass cnts = cnts[0] if imutils.is_cv2() else cnts[1] sd = ShapeDetector() # loop over the contours for c in cnts: # compute the center of the contour, then detect the name of the # shape using only the contour M = cv2.moments(c) #find the middle to return cX = int((M["m10"] / M["m00"])) cY = int((M["m01"] / M["m00"])) shape = sd.detect(c, totalArea) sameObject = False if circlesPres == True and type(shape) == int: if shapesArray[0][1][0] * .98 < cX < shapesArray[0][1][ 0] * 1.02: if shapesArray[0][1][1] * .98 < cY < shapesArray[0][1][
def __init__(self): self.sd = ShapeDetector()
height, width, layers = img.shape # get smaller frame to capture shapes imgSmall = cv2.resize(buffer, (int(width / 4), int(height / 4))) # resize main frame so its easier to see img = cv2.resize(img, (int(width), int(height))) img_copy = img # get ratio to multiply shape contours by when drawing them ratio = img.shape[0] / float(imgSmall.shape[0]) # transform smaller frame to find shape contours gray = cv2.cvtColor(imgSmall, cv2.COLOR_BGR2GRAY) blurred = cv2.GaussianBlur(gray, (5, 5), 0) thresh = cv2.threshold(blurred, 180, 255, cv2.THRESH_BINARY_INV)[1] # initialize shape detecting class sd = ShapeDetector() # get contours from transformed small frame cnts, h = cv2.findContours(thresh.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE) # loop through contours newShapes = [] new_cnts = [c for c in cnts if 300 < cv2.contourArea(c) < 4000] new_cnts = [c for c in new_cnts if cv2.contourArea(c, True) > 0] for c in range(len(new_cnts)): # compute the center of the contour M = cv2.moments(new_cnts[c]) # if going to divide by 0 then skip
def update_frame(self): # global bangun_datar ret,self.image = self.capture.read() self.image = cv2.flip(self.image,1) frame = self.image bangun_datar = '' if ret: buf1 = self.image image = frame resized = imutils.resize(image, width=300) ratio = image.shape[0] / float(resized.shape[0]) adjust_th = 50 if self.ui.edt_nilai_threshold.text() != '': adjust_th = int(self.ui.edt_nilai_threshold.text()) adjust_th_2 = 100 if self.ui.edt_nilai_threshold_2.text() != '': adjust_th_2 = int(self.ui.edt_nilai_threshold_2.text()) gray = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY) blurred = cv2.GaussianBlur(gray, (7, 7), 0) edged = cv2.Canny(blurred, adjust_th, adjust_th_2) edged = cv2.dilate(edged, None, iterations=1) edged = cv2.erode(edged, None, iterations=1) cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts) sd = ShapeDetector() hasil_ = frame for c in cnts: M = cv2.moments(c) if ((M["m10"] == 0.0) and (M["m00"] == 0.0)): print("Kosong") else: print("Berhasil") cX = int(M["m10"] / M["m00"] * ratio) cY = int(M["m01"] / M["m00"] * ratio) shape = sd.detect(c) bangun_datar = shape c = c.astype("float") c *= ratio c = c.astype("int") # cv2.drawContours(image, [c], -1, (0,255,0), 2) cv2.putText(image, shape, (cX, cY), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2) image = frame gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) gray = cv2.GaussianBlur(gray, (7, 7), 0) edged = cv2.Canny(gray, adjust_th, adjust_th_2) edged = cv2.dilate(edged, None, iterations=1) edged = cv2.erode(edged, None, iterations=1) cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts) if (cnts == []): print("Tidak Ada Objek Terdeteksi !") self.displayImage(buf1,1) else: (cnts, _) = contours.sort_contours(cnts) pixelsPerMetric = None for c in cnts: if cv2.contourArea(c) < 100: continue box = cv2.minAreaRect(c) box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box) box = np.array(box, dtype="int") box = perspective.order_points(box) cv2.drawContours(image, [box.astype("int")], -1, (0, 255, 0), 2) for (x, y) in box: cv2.circle(image, (int(x), int(y)), 5, (0, 0, 255), -1) (tl, tr, br, bl) = box (tltrX, tltrY) = midpoint(tl, tr) (blbrX, blbrY) = midpoint(bl, br) (tlblX, tlblY) = midpoint(tl, bl) (trbrX, trbrY) = midpoint(tr, br) cv2.circle(image, (int(tltrX), int(tltrY)), 5, (255, 0, 0), -1) cv2.circle(image, (int(blbrX), int(blbrY)), 5, (255, 0, 0), -1) cv2.circle(image, (int(tlblX), int(tlblY)), 5, (255, 0, 0), -1) cv2.circle(image, (int(trbrX), int(trbrY)), 5, (255, 0, 0), -1) cv2.line(image, (int(tltrX), int(tltrY)), (int(blbrX), int(blbrY)), (255, 0, 255), 2) cv2.line(image, (int(tlblX), int(tlblY)), (int(trbrX), int(trbrY)), (255, 0, 255), 2) dA = dist.euclidean((tltrX, tltrY), (blbrX, blbrY)) dB = dist.euclidean((tlblX, tlblY), (trbrX, trbrY)) if pixelsPerMetric is None: width_OR = 0.955 if self.ui.edt_WOR.text() != '': w_inc = float(self.ui.edt_WOR.text()) if (w_inc != 0.0): width_OR = w_inc / 2.54 pixelsPerMetric = dB / width_OR # pixelsPerMetric = dB / args["width"] dimA = dA / pixelsPerMetric * 2.54 dimB = dB / pixelsPerMetric * 2.54 luas = HitungLuas(bangun_datar, dimA, dimB) if (float(dimB) != 3.0): self.ui.edt_shape_detect.setText(bangun_datar) self.ui.edt_area.setText("{:.1f}cm2".format(luas)) cv2.putText(image, "{:.1f}cm".format(dimA), (int(tltrX - 15), int(tltrY - 10)), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (255, 255, 255), 2) cv2.putText(image, "{:.1f}cm".format(dimB), (int(trbrX + 10), int(trbrY)), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (255, 255, 255), 2) cv2.putText(image, "{:.1f}cm2".format(luas), (int(blbrX + 15), int(blbrY) + 50), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (255, 255, 255), 2) hasil_ = image else: cv2.putText(image, "{:.1f}cm".format(dimB), (int(trbrX + 10), int(trbrY)), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (255, 255, 255), 2) hasil_ = image self.displayImage(hasil_,1)
def get_prediction(): output = defaultdict(list) if request.method == 'POST': # check if the post request has the file part if 'file_to_be_saved' not in request.files: output['status'] = 'false' output['message'] = 'error request' else: file = request.files['file_to_be_saved'] # if user does not select file, browser also submit a empty part without filename if file.filename == '': output['status'] = 'false' output['message'] = 'no file selected' else: if file and allowed_file(file.filename): filename = secure_filename(file.filename) save_path = os.path.join(app.config['UPLOAD_FOLDER'], filename) file.save(save_path) img = Image.open(save_path) base = int(float(img.size[0])/2.5) wpercent = (base / float(img.size[0])) hsize = int((float(img.size[1]) * float(wpercent))) img = img.resize((base, hsize), Image.ANTIALIAS) img.save(save_path, dpi=[100,100]) dump_image = cv2.imread(save_path) # convert the resized image to grayscale, blur it slightly, # and threshold it gray = cv2.cvtColor(dump_image, cv2.COLOR_RGB2GRAY) blurred = cv2.GaussianBlur(gray, (5, 5), 0) thresh = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY)[1] # find contours in the thresholded image and initialize the # shape detector cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[0] if imutils.is_cv2() else cnts[1] sd = ShapeDetector() # loop over the contours shape_feature = {} shape_feature['abstrak'] = 0 shape_feature['kotak'] = 0 shape_feature['persegi'] = 0 shape_feature['lingkaran'] = 0 shape_feature['segitiga'] = 0 shape_feature['segilima'] = 0 shape_feature['segienam'] = 0 for c in cnts: shape = sd.detect(c) if(shape == 'abstrak'): shape_feature['abstrak'] += 1 elif(shape=='kotak'): shape_feature['kotak'] += 1 elif(shape=='persegi'): shape_feature['persegi'] += 1 elif(shape=='lingkaran'): shape_feature['lingkaran'] += 1 elif(shape=='segitiga'): shape_feature['segitiga'] += 1 elif(shape=='segilima'): shape_feature['segilima'] += 1 else: shape_feature['segienam'] += 1 workflow = application.workflows.get('custom-models') if os.path.exists(save_path): open_image = open(save_path,'rb') image = ClImage(file_obj = open_image) translator = Translator() result = [] result = json.loads(json.dumps(workflow.predict([image]))) open_image.close() if result['status']['code'] == 10000: i = 0 for data in result['results']: if data['status']['code'] == 10000: output_feature = defaultdict(list) index = 0 output_feature['url'] = url_for('uploaded_file',filename=filename) output_feature['image'] = filename output_feature['shapes'] = shape_feature for segment in data['outputs']: if index == 0: if len(segment['data']['concepts']) > 0: for item in segment['data']['concepts']: feature = {} translation = translator.translate(item['name'],dest='id') feature['kata'] = translation.text feature['score'] = item['value'] output_feature['features'].append(feature) if index == 1: if len(segment['data']['colors']) > 0: for item in segment['data']['colors']: feature = {} translation = translator.translate(item['w3c']['name'],dest='id') feature['kode'] = item['w3c']['hex'] h = feature['kode'].lstrip('#') rgb = tuple(int(h[j:j+2], 16) for j in (0, 2 ,4)) rgb_schem = {} rgb_schem['r'] = rgb[0] rgb_schem['g'] = rgb[1] rgb_schem['b'] = rgb[2] feature['rgb'] = rgb_schem feature['warna'] = translation.text feature['score'] = item['value'] output_feature['colors'].append(feature) if index == 2: if len(segment['data']['concepts']) > 0: for item in segment['data']['concepts']: feature = {} translation = translator.translate(item['name'],dest='id') feature['label'] = translation.text feature['score'] = item['value'] output_feature['textures'].append(feature) index = index+1 output[i] = output_feature i = i+1 else: output['status'] = 'false' output['message'] = 'file not saved' else: output['status'] = 'false' output['message'] = 'file not accepted' else: output['status'] = 'false' output['message'] = 'error request' return jsonify(output)
def extract_text(img): # Function to extract text boxes, line colors and decipher # text using ocr engine global G # Grab digit images and load them into array # Compute and store histograms for each image digits = pickle.load(open("digits.p", 'rb')) digits2 = pickle.load(open("digits2.p", 'rb')) # Shape detector to tell the shape of contour sd = ShapeDetector() # Filter out ecu boxes ecu_img = line_filter(img, color="ECU", rtype='rgb') ecu_img = cv2.medianBlur(ecu_img, 5) # Enlarge the boxes so we can overwrite the dashed or # solid lines kernel = np.ones((9, 9), np.uint8) _ecu_img = cv2.dilate(ecu_img, kernel, iterations=3) # Grab the contours and then fill them with white to # erase them from the image ecu_contours, centers = get_contours(_ecu_img) no_boxes = clear_contours(img, ecu_contours) ecu_rects = get_bound_rects(ecu_contours) # Convert to gray and filter out the colored wires # so it is just text just_text = bw_filter(no_boxes, rtype='rgb') # Add circle boxes for later relationship building kernel2 = np.ones((5, 5), np.uint8) erode = cv2.erode(just_text, kernel2, iterations=1) median = cv2.medianBlur(erode, 5) temp_contours, circle_centers = get_contours(median) circle_centers = list(set(circle_centers)) # Loop through and grab only circle contours circle_centers = [] for c in temp_contours: if sd.detect(c) == "circle": M = cv2.moments(c) if M["m00"] == 0: continue cX = int(M["m10"] / M["m00"]) cY = int(M["m01"] / M["m00"]) circle_centers.append((cX, cY)) circle_centers = circle_centers # Dilate text to get a rectangular contour # (Thanksgiving Approach) enlarged = cv2.dilate(just_text, kernel, iterations=5) # Grab text contours and convert to rectangular # boundries text_contours, centers = get_contours(enlarged) params = get_bound_rects(text_contours) # Decipher/Filter text and store mappings to # center and bounding rectangle ecu_map = {} decoded_text = [] brgb = Image.fromarray(img, 'RGB') for i, _roi in enumerate(params): roi = list(_roi) roi[2] += roi[0] roi[3] += roi[1] roi = tuple(roi) ar = float(_roi[2]) / _roi[3] text_roi = brgb.crop(roi) text = pytesseract.image_to_string(text_roi) if lc_check(text): pass elif len(text) == 0 and (.65 < ar < 1.25): text_roi = text_roi.resize((100, 80)).convert('L') best_score = 0 best_index = 0 for i2, d in enumerate(digits): cmp_score = compare_ssim(d, np.array(text_roi), data_range=d.max() - d.min()) cmp_score2 = compare_ssim(digits2[i2], np.array(text_roi), data_range=d.max() - d.min()) score = max(cmp_score, cmp_score2) if score > best_score: best_score = score best_index = i2 + 1 if _roi[0] < (200): ecu_map[i] = (_roi, "IN-" + str(best_index)) elif _roi[0] > (img.shape[1] - 200): ecu_map[i] = (_roi, "OUT-" + str(best_index)) else: # Check if text passes filter test if filter_text(text) != "": if text in decoded_text: continue else: decoded_text.append(text) (closest_ecu, text_near) = text2_ecu(ecu_rects, _roi, text) if not any(closest_ecu): pass else: ecu_map[i] = (closest_ecu, text_near) # Create nodes for each ecu and outward/inward for k, v in ecu_map.items(): val = v[1].replace("\n\n", "\n") G.add_node(val) return ecu_map
cam_center = (size[1] / 2, size[0] / 2) zero_camera_matrix = np.array( [[focal_length, 0, cam_center[0]], [0, focal_length, cam_center[1]], [0, 0, 1]], dtype=np.float32) zero_distort_matrix = np.zeros((4, 1)) #frame = undistort_img(frame, cameraMatrix, distortMatrix) frame_hsv = rid_noise(frame) # find contours in thresholded frame, then grab the largest one cnts = cv2.findContours(frame_hsv.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) cnts = imutils.grab_contours(cnts) sd = ShapeDetector() if cnts is not None and (len(cnts) > 0): # prints number of contours found to the monitor print("found contours", len(cnts)) # find the biggest contour in the screen (the closest) c = find_best_contour(cnts, mid_X_frame) # acquire corner points of the contour cPoints = get_corners(frame_corners, c) if c is not None and len(c) != 0: shape = sd.detect(c)
def CoordTransformer(image, img_filename): # 灰度化 gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # 高斯模糊 blurred = cv2.GaussianBlur(gray, (5, 5), 0) # 二值化 _, thresh = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY) # 轮廓检测 cnts = cv2.findContours(255 - thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts) # 形状检测 sd = ShapeDetector() output_image = image.copy() detections = [] for c in cnts: shape = sd.detect(c) shape_dict = {"circle": 0, "triangle": 3, "rectangle": 4} M = cv2.moments(c) cX = int(M["m10"] / M["m00"]) cY = int(M["m01"] / M["m00"]) detections.append([shape_dict[shape], cY, cX]) c = c.astype("int") cv2.drawContours(output_image, [c], -1, (0, 255, 0), 2) cv2.putText(output_image, shape, (cX, cY), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2) cv2.imwrite("images/{}_detected.jpg".format(img_filename), output_image) # 坐标变换 clf = linear_model.Ridge() clf = joblib.load("model/coord_calib_ridge_regression.pkl") detect_array = np.array(detections, dtype="float32") print("-------------------------") print("Positions in Camera coord") print(detect_array) detect_array[:, 1:] = clf.predict(detect_array[:, 1:]) detect_bytes = [ "{:.0f}{:0>7.2f}{:0>7.2f}".format(row[0], row[1], row[2]).encode() for row in detect_array ] print("Positions in Robot coord ") print(detect_bytes) print("-------------------------") return detect_bytes
ratio = image.shape[0] / float(resized.shape[0]) blurred = cv2.GaussianBlur(resized, (5, 5), 0) gray = cv2.cvtColor(blurred, cv2.COLOR_BGR2GRAY) lab = cv2.cvtColor(blurred, cv2.COLOR_BGR2LAB) thresh = cv2.threshold(gray, 170, 255, cv2.THRESH_BINARY)[1] # thresh = cv2.Canny(gray, 50, 100) # thresh = cv2.dilate(thresh, None, iterations=2) # thresh = cv2.erode(thresh, None, iterations=2) cv2.imshow("Thresh", thresh) cv2.waitKey(0) cnts = cv2.findContours(thresh.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts) (cnts, _) = contours.sort_contours(cnts) sd = ShapeDetector() cl = ColorLabeler() # for c in cnts: if 1: if len(cnts) > 1: c = cnts[1] else: c = cnts[0] M = cv2.moments(c) cX = int((M["m10"] / M["m00"]) * ratio) cY = int((M["m01"] / M["m00"]) * ratio) shape, approx = sd.detect(c) color, color_value = cl.label(lab, c) # mean = cl2.get_mean(lab, c)
def Platoon(): lower = np.array([0, 164, 169], dtype = "uint8") upper = np.array([138, 255, 255], dtype = "uint8") sd = ShapeDetector() os.system("pkill uv4l") cap = cv2.VideoCapture(1) f_count = 0 Forward(50) #Starting car here while(cap.isOpened()): if getattr(thread, "platooning", True) == False: break # Capture frame-by-frame ret, frame = cap.read() #frame=cv2.flip(frame, -1) f_count += 1 if ret == True and f_count == 1: #adjust here for skipping frames f_count = 0 #resize frame resized = imutils.resize(frame, width=640, height=480) #color mask mask = cv2.inRange(resized, lower, upper) cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts) #variable to store biggest contour biggest_c = None # loop over the contours for c in cnts: #if it fits criteria, update biggest_c variable with current contour if sd.detect(c, biggest_c): biggest_c = c if biggest_c is not None:#if contour found M = cv2.moments(biggest_c) cX = int(M["m10"] / M["m00"]) cX_track = cX-320 #find difference in pixels from center peri = int(cv2.arcLength(biggest_c, True)) if peri > 50: angle = int(cX_track / 320 * 50) #-50 to 50 degrees steering. Might need to flip sign #print("Steer("+str(angle)+")") #command to arduino if angle > -50 and angle < 50: Steer(int(angle)) #print(int(angle)) #STEERING speed = 70 * 2 * (90/peri) + 30 if 0 < speed and 255 > speed: if speed < 65: Stop() else: Forward(int(speed)) print(int(speed)) # Break the loop elif ret == False: break # When everything done, release the video capture object cap.release() Stop()
frame = vs.read() height, width = frame.shape[0:2] scaleFactor = 4 newWidth, newHeight = width // scaleFactor, height // scaleFactor resizedColor = cv2.resize(frame, (newWidth, newHeight), interpolation=cv2.INTER_CUBIC) blurred = cv2.GaussianBlur(resizedColor, (3, 3), 0) hsv = cv2.cvtColor(blurred, cv2.COLOR_BGR2HSV) mask = cv2.inRange(hsv, redLower, redUpper) mask = cv2.erode(mask, None, iterations=2) mask = cv2.dilate(mask, None, iterations=2) cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts) sd = ShapeDetector() center = None x = 0 radius = 0 if len(cnts) > 0: for c in cnts: if (sd.detect(c)): ((x, y), radius) = cv2.minEnclosingCircle(c) if (int(radius) > 6): cv2.circle(resizedColor, (int(x), int(y)), int(radius), (0, 255, 255), 2) x = int(x) radius = int(radius) packet = '<{}, {}>'.format(x, radius) packetBytes = bytes(packet, 'utf-8')
def determine_move(self, tof): # Capture image with RBP camera sleep(5) self.camera.capture('/home/jon_pi/Desktop/esc204/vision/align_w_port/test.jpg') # Read image with OpenCV image = cv.imread('test.jpg') # Define bounds on blue colour segmentation red = ([40,20,80], [60,40,120]) # Perform colour segmentation (lower, upper) = red lower = np.array(lower, dtype = "uint8") upper = np.array(upper, dtype = "uint8") mask = cv.inRange(image, lower, upper) output = cv.bitwise_and(image, image, mask = mask) '''new = cv.resize(output, (int(2592/3),int(1944/3))) cv.imshow("Blue only", new) cv.waitKey(0)''' # Convert colour segmented image to grayscale gray = cv.cvtColor(output, cv.COLOR_BGR2GRAY) # blurred = cv.GaussianBlur(gray, (5, 5), 0) # Convert image again to black and white only thresh = cv.threshold(gray, 60, 255, cv.THRESH_BINARY)[1] '''new = cv.resize(thresh, (int(2592/3),int(1944/3))) cv.imshow("Threshold", new) cv.waitKey(0)''' # Detect shapes in image cnts = cv.findContours(thresh.copy(), cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts) sd = ShapeDetector() # Empty array to collect data points of detected rectangles array = [] # Loop through detected shapes for c in cnts: M = cv.moments(c) cX = int((M["m10"] / (M["m00"] + 1e-7))) cY = int((M["m01"] / (M["m00"] + 1e-7))) shape = sd.detect(c) # Only add shape to list if it is a large rectangle if (shape == "rectangle") and (len(c) > 40): array.append(c) c = c.astype("int") cv.drawContours(image, [c], -1, (0, 255, 0), 2) cv.putText(image, shape, (cX, cY), cv.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2) '''new = cv.resize(image, (int(2592/3),int(1944/3))) cv.imshow("Image", new) cv.waitKey(0)''' # Empty list to collect post-processed data data = [] # Collect x coordinates from first rectangle for i in array[0]: data.append(i[0][0]) # Collect x coordinates from second rectangle for j in array[1]: data.append(j[0][0]) # Find x position of outermost edges positions = self.outer_positions(data) move = self.x_pos(positions) xDist = self.convert_dist(move[1],tof) # -1 -> move left, 1 -> move right, 0 -> don't move; magnitude of move in mm return(move[0], xDist) # Print movement for humans to see # print(move(positions[0],positions[1])) # Display image of detected rectangles '''new = cv.resize(image, (int(2592/3),int(1944/3)))
gray = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY) kernel = np.ones((5, 5), np.uint8) gray = cv2.dilate(gray, kernel, iterations=3) cv2.imshow('gray', gray) blurred = cv2.GaussianBlur(gray, (5, 5), 0) thresh = cv2.threshold(blurred, 50, 250, cv2.THRESH_BINARY)[1] cv2.imshow('T', thresh) # find contour image, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnt = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR) cv2.drawContours(cnt, contours, -1, (0, 255, 0), 2) sd = ShapeDetector() dst = img # find center of object for c in range(len(contours)): M = cv2.moments(contours[c]) rect = cv2.minAreaRect(contours[c]) box = cv2.boxPoints(rect) box = np.int0(box) angle = int(rect[2]) # Box2D get (x,y),(width,height),theta cv2.drawContours(dst, [box], 0, (0, 0, 255), 2) # shape = sd.detect(c) cx = 0 cy = 0 if (M['m00'] != 0):
def takePicture(): for i in gpios: GPIO.output(i, GPIO.LOW) global image_saveIdx, storeMode camera.annotate_bg = False camera.annotate_text = '' while True: filename = pathData[storeMode][ 0] + '/IMG_' + '%04d' % image_saveIdx + '.JPG' if not os.path.isfile(filename): break image_saveIdx += 1 if image_saveIdx > 9999: image_saveIdx = 0 camera.capture(filename, use_video_port=False, format='jpeg', thumbnail=None) os.chmod(filename, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH) showImage(image_saveIdx) start = time.clock() image = cv2.imread(filename) resized = imutils.resize(image) #note ratio = image.shape[0] / float(resized.shape[0]) gray = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY) blurred = cv2.GaussianBlur(gray, (5, 5), 0) thresh = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY)[1] cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[0] if imutils.is_cv2() else cnts[1] sd = ShapeDetector() #end = time.clock() a5 = 0 try: length = 0 for c in cnts: a1 = time.time() area = cv2.contourArea(c) M = cv2.moments(c) cX = int((M["m10"] / M["m00"]) * ratio) cY = int((M["m01"] / M["m00"]) * ratio) shape = sd.detect(c) a2 = time.time() if shape == 'triangle': corners = cv2.goodFeaturesToTrack(gray, 25, 0.01, 10) length = len(corners) if length == 9: GPIO.output(beep, GPIO.HIGH) time.sleep(0.1) GPIO.output(beep, GPIO.LOW) time.sleep(0.1) GPIO.output(beep, GPIO.HIGH) time.sleep(0.1) GPIO.output(beep, GPIO.LOW) part = round(0.7 + (a2 - a1) * 100, 3) print shape, "damaged!!!", "area is", area, "part of", part, "%" #print corners #print length #camera.annotate_text = '' elif length == 10: GPIO.output(beep, GPIO.HIGH) time.sleep(0.1) GPIO.output(beep, GPIO.LOW) time.sleep(0.1) GPIO.output(beep, GPIO.HIGH) time.sleep(0.1) GPIO.output(beep, GPIO.LOW) part = round(2.5 + (a2 - a1) * 100, 3) print shape, "damaged!!!", "area is", area, "part of", part, "%" #print corners #print length else: if a5 == 0: print shape, "area is", area #print length else: print shape, "area is", area else: if a5 == 0: print shape, "area is", area else: print shape, "area is", area a3 = time.time() c = c.astype("float") c *= ratio c = c.astype("int") cv2.drawContours(image, [c], -1, (0, 0, 255), 2) cv2.putText(image, shape, (cX, cY), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 1) cv2.imwrite(filename, image) end = time.clock() all_time = end - start camera.annotate_text = 'Detection successfully concluded! Used {0}s'.format( all_time) if shape == 'triangle': if length >= 9: #camera.annotate_text = shape,'damaged!!!','area is',area,'part of',part,'%' camera.annotate_text = 'The triangle is damaged!!!area is {0} part of {1} % '.format( area, part) GPIO.output(beep, GPIO.HIGH) time.sleep(0.1) GPIO.output(beep, GPIO.LOW) except: camera.annotate_text = 'Detection failed, please check the detection area!'
elif blurtype == 2: blurred = cv2.blur(image, (blur, blur)) elif blurtype == 3: blurred = cv2.bilateralFilter(image, 9, blur, blur) gray = cv2.cvtColor(blurred, cv2.COLOR_BGR2GRAY) ret, thresh = cv2.threshold(gray, threshold_min, 255, cv2.THRESH_BINARY) if invert == 1: thresh = cv2.bitwise_not(thresh) contours = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) contours = contours[0] if imutils.is_cv2() else contours[1] sd = ShapeDetector() # loop over the contours for contour in contours: M = cv2.moments(contour) cX = 50 cY = 50 if M["m00"] != 0: cX = int((M["m10"] / M["m00"]) * ratio) cY = int((M["m01"] / M["m00"]) * ratio) shape = sd.detect(contour) # multiply the contour (x, y)-coordinates by the resize ratio, # then draw the contours and the name of the shape on the image contour = contour.astype("float")
args = vars(ap.parse_args()) # load the image, convert it to grayscale, blur it slightly, # and threshold it image = cv2.imread(args["image"]) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) blurred = cv2.GaussianBlur(gray, (5, 5), 0) thresh = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1] # find contours in the thresholded image cnts = cv2.findContours(thresh.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) cnts = cnts[0] if imutils.is_cv2() else cnts[1] # and initialize the shape detector sd = ShapeDetector() # shapes, center and perimeter array scp = [] # Start the shapes counters triangle = 0 square = 0 rectangle = 0 pentagon = 0 unidentified = 0 # loop over the contours for c in cnts: M = cv2.moments(c)
def findobject(nColor): with Switch(nColor) as case: if case(Color.RED.value): # red InRange_L = np.array([0, 89, 255], dtype=np.uint8) InRange_H = np.array([7, 255, 255], dtype=np.uint8) if case(Color.YELLOW.value): # yellow InRange_L = np.array([21, 144, 144], dtype=np.uint8) InRange_H = np.array([180, 255, 255], dtype=np.uint8) if case(Color.GREEN.value): # green InRange_L = np.array([61, 30, 255], dtype=np.uint8) InRange_H = np.array([74, 255, 255], dtype=np.uint8) if case(Color.BLUE.value): # blue InRange_L = np.array([86, 57, 255], dtype=np.uint8) InRange_H = np.array([115, 255, 255], dtype=np.uint8) if case(Color.ORANGE.value): # orange InRange_L = np.array([28, 74, 255], dtype=np.uint8) InRange_H = np.array([43, 112, 255], dtype=np.uint8) _, frame = cap.read() img = frame hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) target = cv2.inRange(hsv, InRange_L, InRange_H) mask = cv2.bitwise_and(frame, frame, mask=target) gray = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY) kernel = np.ones((5, 5), np.uint8) gray = cv2.dilate(gray, kernel, iterations=1) # cv2.imshow('gray',gray) blurred = cv2.GaussianBlur(gray, (5, 5), 0) thresh = cv2.threshold(blurred, 50, 250, cv2.THRESH_BINARY)[1] # cv2.imshow('T', thresh) # find contour image, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnt = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR) cv2.drawContours(cnt, contours, -1, (0, 255, 0), 2) sd = ShapeDetector() dst = img # find center of object for c in range(len(contours)): M = cv2.moments(contours[c]) rect = cv2.minAreaRect(contours[c]) box = cv2.boxPoints(rect) box = np.int0(box) angle = int(rect[2]) # Box2D get (x,y),(width,height),theta cv2.drawContours(dst, [box], 0, (0, 0, 255), 2) # shape = sd.detect(c) cx = 0 cy = 0 if (M['m00'] != 0): cx = int(M['m10'] / M['m00']) cy = int(M['m01'] / M['m00']) if cx != 320: xPos = Px2mm(cx) if cy != 240: yPos = Py2mm(cy) zPos = 213 cv2.circle(dst, (cx, cy), 7, (0, 0, 0), -1) Pos = str(cx) + ',' + str(cy) + ',' + str(angle) cv2.putText(dst, Pos, (cx - 30, cy - 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1) # count += 1 # if (xPos != 0): # print (yPos) # if (count == 25): # control.move((-116)-xPos, yPos - 539, zPos) control.move(cpPosX - xPos, cpPosY + yPos, zPos) time.sleep(3) control.move(cpPosX - xPos, cpPosY + yPos, 108) time.sleep(2) control.pick(0) time.sleep(1) control.move(cpPosX - xPos, cpPosY + yPos, zPos) time.sleep(3) control.move2Jig(-375.8, -361, 226.9, 2.22, 0.62, -1.07) time.sleep(2) control.move2Jig(-383.7, -310, 125.1, 2.48, 0.5, -1.22) time.sleep(2) control.place(0) time.sleep(1) control.move(cpPosX, cpPosY, cpPosZ) time.sleep(2.5) control.startLeftArm(0) time.sleep(1) control.stopLeftArm(0)
from shapedetector import ShapeDetector import cv2 from matplotlib import pyplot as plt image= cv2.imread("images/shapes_and_colors.jpg") original=image.copy() gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) blurred=cv2.GaussianBlur(gray,(5,5),0) thresh=cv2.threshold(blurred,60,255,cv2.THRESH_BINARY)[1] cnts=cv2.findContours(thresh.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) cnts=cnts[1] sd=ShapeDetector() for c in cnts: M=cv2.moments(c) cX=0 cY=0 if(M["m00"])!=0: cX=int(M["m10"]/M["m00"]) cY=int(M["m01"]/M["m00"]) shape=sd.detect(c) cv2.drawContours(image,[c],-1,(0,255,0),2) cv2.circle(image,(cX,cY),7,(255,255,255),2) cv2.putText(image, shape, (cX - 20, cY - 20),cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2) plt.subplot(2,2,1),plt.imshow(original,cmap = 'gray'),plt.title('Original') plt.axis("off") plt.subplot(2,2,2),plt.imshow(thresh,cmap="gray"),plt.title('Threshold') plt.axis("off")
image = cv2.imread('rectangles.jpg') print(image) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) blurred = cv2.GaussianBlur(gray, (5, 5), 0) thresh = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY)[1] ksize = 5 kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (ksize,ksize)) thresh = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel) cnts = cv2.findContours(thresh.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) # ~ cnts = cnts[0] if imutils.is_cv2() else cnts[1] cnts = cnts[1] sd = ShapeDetector() for c in cnts: M = cv2.moments(c) if M["m00"] != 0: # prevent divide by zero cX = int((M["m10"] / M["m00"])) cY = int((M["m01"] / M["m00"])) shape = sd.detect(c) c = c.astype("float") #c *= ratio c = c.astype("int") cv2.drawContours(image, [c], -1, (0, 255, 0), 2) cv2.putText(image, shape, (cX, cY), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
class PartDetector: def __init__(self): self.sd = ShapeDetector() def detect(self, frame, markInFrame=False): resized = imutils.resize(frame, width=300) ratio = frame.shape[0] / float(resized.shape[0]) contours = self._find_contour(resized) if len(contours) == 0: return None, None contour, center = self._Filter_n_find_center(contours, resized.shape, ratio) if contour is None: return None, None shape = self.sd.detect(contour) if shape is None: return None, None if markInFrame: c = contour.astype("float") c *= ratio c = c.astype("int") self._mark_object(frame, c, center, shape) return shape, center def _find_contour(self, frame): #cv2.imshow('frame',frame) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #cv2.imshow('gray',gray) blurred = cv2.GaussianBlur(gray, (5, 5), 0) #blurred = cv.medianBlur(gray, 5) #cv2.imshow('blurred',blurred) #clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8)) #hist_equal = clahe.apply(blurred) hist_equal = cv2.equalizeHist(blurred) #cv2.imshow('hist_equal',hist_equal) ret, thresh = cv2.threshold( hist_equal, 50, 255, cv2.THRESH_BINARY_INV) # + cv2.THRESH_OTSU) thresh = cv2.adaptiveThreshold(blurred, 255, cv2.ADAPTIVE_THRESH_MEAN_C,\ cv2.THRESH_BINARY_INV,11,2) thresh = cv2.adaptiveThreshold(blurred, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\ cv2.THRESH_BINARY_INV,11,2) """ thresh[:,:60] = 0 thresh[:,-60:] = 0 thresh[:60,:] = 0 thresh[-60:,:] = 0 """ #cv2.imshow('thresh',thresh) # Taking a matrix of size 5 as the kernel #kernel = np.ones((3,3), np.uint8) # The first parameter is the original image, # kernel is the matrix with which image is # convolved and third parameter is the number # of iterations, which will determine how much # you want to erode/dilate a given image. #img_erosion = cv2.erode(thresh, kernel, iterations=1) #thresh = cv2.dilate(img_erosion, kernel, iterations=1) #cv2.imshow('erode_dilate',thresh) cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts) return cnts def _Filter_n_find_center(self, cnts, shape, ratio=1): for c in cnts: M = cv2.moments(c) if M["m00"] == 0: continue cX = int((M["m10"] / M["m00"]) * ratio) cY = int((M["m01"] / M["m00"]) * ratio) area = M['m00'] frameArea = shape[0] * shape[1] areaRatio = area / frameArea if areaRatio < 0.01 or areaRatio > 0.1: continue return c, [cX, cY] return None, None def _mark_object(self, frame, contour, center, shape, ratio=1): cv2.circle(frame, tuple(center), 5, (0, 255, 0)) cv2.drawContours(frame, [contour], -1, (0, 255, 0), 2) cv2.putText(frame, shape, tuple(center), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)