def visualize_result(image, edge_image, shape, closest_points): output_image = 0.5 * (image + edge_image[:, :, np.newaxis]) points = closest_points[:, [1, 0]] perimeter = draw.polygon_perimeter(points[:, 0], points[:, 1]) draw.set_color(output_image, (perimeter[0].astype(np.int), perimeter[1].astype(np.int)), [0, 1, 0]) points = shape[:, [1, 0]] perimeter = draw.polygon_perimeter(points[:, 0], points[:, 1]) draw.set_color(output_image, (perimeter[0].astype(np.int), perimeter[1].astype(np.int)), [0, 0, 1]) return output_image
def test_polygon_perimeter(): expected = np.array( [[1, 1, 1, 1], [1, 0, 0, 1], [1, 1, 1, 1]] ) out = np.zeros_like(expected) rr, cc = polygon_perimeter([0, 2, 2, 0], [0, 0, 3, 3]) out[rr, cc] = 1 assert_array_equal(out, expected) out = np.zeros_like(expected) rr, cc = polygon_perimeter([-1, -1, 3, 3], [-1, 4, 4, -1], shape=out.shape, clip=True) out[rr, cc] = 1 assert_array_equal(out, expected) assert_raises(ValueError, polygon_perimeter, [0], [1], clip=True)
# x = shape[1] # xExtent = int(x * bounds[2] / 100) # y = shape[0] # yExtent = int(y * bounds[3] / 100) yExtent = int(bounds[3]) xEntent = int(bounds[2]) # Coordinates are around the center xCoord = int(bounds[0] - bounds[2] / 2) yCoord = int(bounds[1] - bounds[3] / 2) boundingBox = [[xCoord, yCoord], [xCoord, yCoord + yExtent], [xCoord + xEntent, yCoord + yExtent], [xCoord + xEntent, yCoord]] # Wiggle it around to make a 3px border rr, cc = draw.polygon_perimeter( [x[1] for x in boundingBox], [x[0] for x in boundingBox], shape=shape) rr2, cc2 = draw.polygon_perimeter( [x[1] + 1 for x in boundingBox], [x[0] for x in boundingBox], shape=shape) rr3, cc3 = draw.polygon_perimeter( [x[1] - 1 for x in boundingBox], [x[0] for x in boundingBox], shape=shape) rr4, cc4 = draw.polygon_perimeter( [x[1] for x in boundingBox], [x[0] + 1 for x in boundingBox], shape=shape) rr5, cc5 = draw.polygon_perimeter( [x[1] for x in boundingBox],
# find points inside each region poly = vertices[region] rr, cc = polygon(poly[:, 0], poly[:, 1], img.shape) # find dominant color using k means clustering colors = img[rr, cc] kmeans = KMeans(n_clusters=3, random_state=0) labels = kmeans.fit_predict(colors) #count labels to find most popular label_counts = Counter(labels) #subset out most popular centroid dominant_color = kmeans.cluster_centers_[label_counts.most_common(1)[0][0]] x = dominant_color[0] y = dominant_color[1] z = dominant_color[2] # colorize the inside img[rr, cc] = (x, y, z) # colorize the perimeter rr, cc = polygon_perimeter(poly[:, 0], poly[:, 1], img.shape) img[rr, cc] = (0, 0, 0) # Back to rgb and save img = lab2rgb(img) io.imsave(save_path, img) img = Image.open(save_path) img = img.resize((1000, 1000), 0) img.save('FinalResult.jpg') os.remove('lessPixels.jpg')
def poly2rect(poly): '''Approximates a polygon with an axis-aligned rectangle by minimising the mean of point-to-polygon distances of all points on the rectangle. Parameters ---------- poly : numpy.array a list of 2D points forming a single polygon Returns ------- rect : geomt.rect a 2D rectangle with integer coordinates ''' if len(poly.shape) != 2 or poly.shape[1] != 2: raise ValueError( "Only accepting a numpy array of shape (:,2) as a polygon. Receiving shape {}" .format(poly.shape)) # preparation poly = poly.astype(_np.int32) # make the coordinates integer N = poly.shape[0] tl = poly.min(axis=0) br = poly.max(axis=0) # special cases if N == 0: return rect() if N <= 2 or tl[0] >= br[0] or tl[1] >= br[1]: return rect(min_x=tl[0], min_y=tl[1], max_x=br[0], max_y=br[1]) # convexify if necessary if N > 3: poly = poly[_ss.ConvexHull(poly).vertices] N = poly.shape[0] tl = poly.min(axis=0) br = poly.max(axis=0) # draw polygon perimeter w, h = (br - tl) + 1 poly -= tl # to make poly non-negative coordinates buf = _np.ones((h, w), dtype=_np.uint8) rr, cc = _sd.polygon_perimeter(poly[:, 1], poly[:, 0], shape=buf.shape, clip=True) buf[rr, cc] = 0 # compute distance transform edt = _sn.distance_transform_edt(buf) # compute the integral image img = _sti.integral_image(edt) # optimise r0 = rect(min_x=0, min_y=0, max_x=w - 1, max_y=h - 1) loss0 = rect_perimeter(img, r0) dirty = True while dirty: dirty = False # top if r0.min_y + 1 < r0.max_y: r = rect(min_x=r0.min_x, min_y=r0.min_y + 1, max_x=r0.max_x, max_y=r0.max_y) loss = rect_perimeter(img, r) if loss < loss0: r0 = r loss0 = loss dirty = True # left if r0.min_x + 1 < r0.max_x: r = rect(min_x=r0.min_x + 1, min_y=r0.min_y, max_x=r0.max_x, max_y=r0.max_y) loss = rect_perimeter(img, r) if loss < loss0: r0 = r loss0 = loss dirty = True # bottom if r0.min_y + 1 < r0.max_y: r = rect(min_x=r0.min_x, min_y=r0.min_y, max_x=r0.max_x, max_y=r0.max_y - 1) loss = rect_perimeter(img, r) if loss < loss0: r0 = r loss0 = loss dirty = True # right if r0.min_x + 1 < r0.max_x: r = rect(min_x=r0.min_x, min_y=r0.min_y, max_x=r0.max_x - 1, max_y=r0.max_y) loss = rect_perimeter(img, r) if loss < loss0: r0 = r loss0 = loss dirty = True return rect(min_x=r0.min_x + tl[0], min_y=r0.min_y + tl[1], max_x=r0.max_x + tl[0], max_y=r0.max_y + tl[1])
# Compile images cm = matplotlib.cm.ScalarMappable(None, cmap='plasma') cm.set_clim(0.00, 0.08) disparities = np.load(os.path.join(cfg['disp_path'], cfg['disp_fmt'].format(cfg['filenames_index'])), mmap_mode='r') full_image = np.zeros((2 * len(cfg['sets']) * cfg['image_size'][0], len(cfg['distances']) * cfg['image_size'][1], 3), dtype=np.uint8) for si, s in enumerate(cfg['sets']): print('Set: {}'.format(s)) imgs = np.zeros((cfg['image_size'][0], len(cfg['distances']) * cfg['image_size'][1], 3), dtype=np.uint8) disps = np.zeros_like(imgs) for di, d in enumerate(cfg['distances']): fn = cfg['filename_fmt'].format(set=s, scene=cfg['scene'], object=cfg['object'], distance=d) mask_fn = cfg['mask_fmt'].format(set=s, scene=cfg['scene'], object=cfg['object'], distance=d) img = imread(os.path.join(cfg['data_path'], fn)) img = img_as_ubyte(resize(img, cfg['image_size'])) imgs[:, (di * cfg['image_size'][1]):((di + 1) * cfg['image_size'][1])] = img mask = imread(os.path.join(cfg['data_path'], mask_fn))[:, :, 0] mask = resize(mask, cfg['image_size']) c = find_contours(mask, 0.5)[0] disp = disparities[indices[(s, d)], :, :] disp = resize(disp, cfg['image_size']) disp = img_as_ubyte(cm.to_rgba(disp)[:, :, :3]) rr, cc = polygon_perimeter(c[:, 0], c[:, 1]) outline = np.zeros(cfg['image_size']) outline[rr, cc] = 1 outline = binary_dilation(outline, square(5)) disp[outline, :] = [255, 255, 255] disps[:, (di * cfg['image_size'][1]):((di + 1) * cfg['image_size'][1])] = disp full_image[((si * 2) * cfg['image_size'][0]):((si * 2 + 1) * cfg['image_size'][0]), :] = imgs full_image[((si * 2 + 1) * cfg['image_size'][0]):((si * 2 + 2) * cfg['image_size'][0]), :] = disps imsave(cfg['output_path'], full_image)
def crop_and_save_roofs(fp_interim='', fp_processed='', size=500): """ This method does following processes on the tiff-files located in fp_interim to generate the roofs datasets in fp_processed: - Read the tiff-file located in the fp_interim path for the country and place - Read the GeoJSON files containing the geometries for different roofs - Convert the geometries to the same crs as the tiff file - Creates a box around the centroid of the geometry - Crop and save that box in train_valid_test_area - Load the box and mask the geometry - Draw the geometry on the box and save it in train_valid_test_coontours - Crop and safe the roof in train_valid_test_roofs - Create features - Add entry to the train_valid_test.csv params: :param fp_interim: the path to interin data :param fp_processed: the path to processed data :param size: the size in m2 of the area to crop the roof """ locations = [{ 'country': 'colombia', 'place': 'borde_rural' }, { 'country': 'colombia', 'place': 'borde_soacha' }, { 'country': 'guatemala', 'place': 'mixco_1_and_ebenezer' }, { 'country': 'guatemala', 'place': 'mixco_3' }, { 'country': 'st_lucia', 'place': 'castries' }, { 'country': 'st_lucia', 'place': 'dennery' }, { 'country': 'st_lucia', 'place': 'gros_islet' }] for loc in locations: country = loc['country'] place = loc['place'] fp_interim_place = f'{fp_interim}stac/{country}/{place}/' print( f'{"-" * 120}\nStart processing: {country}\t\t{place}\n{"-" * 120}' ) # Load train and test GEOjson in a GeoDataFrame. Columns: id, roof_material, verified, geometry train_geo_df = gpd.read_file( f'{fp_interim_place}train-{place}.geojson') test_geo_df = gpd.GeoDataFrame( ) # Neither the Castries and Gros Islet have a "test-" GeoJSON file. if place not in ['castries', 'gros_islet']: test_geo_df = gpd.read_file( f'{fp_interim_place}test-{place}.geojson') # Open the Tiff file tiff_file = rasterio.open(f'{fp_interim_place}{place}_ortho-cog.tif', 'r') tiff_crs = tiff_file.crs.data # Convert the train geometry crs to the tiff crs train_geo_df.to_crs( crs=tiff_crs, inplace=True ) # Todo: PyProj FutureWarning: '+init=<authority>:<code>' syntax is deprecated. # Convert the test geometry crs, if it exists, to the tiff crs if not test_geo_df.empty: test_geo_df.to_crs( crs=tiff_crs, inplace=True ) # Neither the Castries and Gros Islet have a "test-" GeoJSON file. # Prepare the profile for the cropped image profile = tiff_file.profile for is_test, geo_df in zip([False, True], [train_geo_df, test_geo_df]): # check if csv-files exists, if not create it if not os.path.isfile(fp_processed + 'train_valid_test.csv'): fieldnames = ['id', 'country', 'place', 'verified', 'area', 'complexity', 'x', 'y'] + \ ['z_min', 'z_max', 'z_median', 'z_count', 'z_majority', 'z_minority', 'z_unique', 'z_range', 'z_sum'] + \ ['label', 'test'] with open(fp_processed + 'train_valid_test.csv', 'w') as csv_file: writer = csv.DictWriter(csv_file, fieldnames=fieldnames) writer.writeheader() # Loop through each train/test roof print(f'Cropping roofs') for _, roof in geo_df.iterrows(): roof_id = roof['id'] roof_geometry = roof['geometry'] if is_test: roof_label = None roof_verified = False else: roof_label = roof['roof_material'] roof_verified = roof['verified'] print(roof_id, end=' ', flush=True) # First create a box of size m2 around the center of the roof centroid = roof_geometry.centroid box = centroid.buffer(np.sqrt(size) / 2, cap_style=3) # 3 -> box # Crop the tiff image box, transform = rasterio.mask.mask(tiff_file, [box], crop=True) profile[ 'count'] = 3 # We'll save the box in RGB and not RGB+Alpha profile['driver'] = 'PNG' profile['width'] = box.shape[1] profile['height'] = box.shape[2] profile['transform'] = transform # Remove profile keys that doen't exist in PNG file [ profile.pop(key, None) for key in [ 'blockxsize', 'blockysize', 'tiled', 'compress', 'interleave' ] ] # Save the box to png file box = box[: 3, ::] # destroy the alpha channel for saving: 4*520*520 to 3*520*520 with rasterio.open( f'{fp_processed}train_valid_test_areas/{roof_id}.png', 'w', **profile) as png_file: png_file.write(box) # Second mask the roof geometry from the box => image size is same as box box_file = rasterio.open( f'{fp_processed}train_valid_test_areas/{roof_id}.png', 'r') roof, _ = rasterio.mask.mask(box_file, [roof_geometry], crop=False) roof = roof.transpose(1, 2, 0) # Draw the contours roof_gray = rgb2gray(roof) # 520*520 contours = measure.find_contours(roof_gray, .01) # Assuming the roof contour is the fist contour, others are just artifacts if len( contours ) == 0: # The geometry lies completely outside the box. Draw contour around the box h = np.arange(1, box.shape[1]) w = np.arange(1, box.shape[1]) s = min( box.shape[1:]) # the smallest x,y coordinate of box h_coord = np.concatenate([h, [3] * s, h, [max(h) - 3] * s]) w_coord = np.concatenate([[3] * s, w, [max(w) - 3] * s, w]) contour = np.array([[x, y] for x, y in zip(h_coord, w_coord)]) else: contour = contours[0] # Draw the contour with line_size in the box line_size = [-2, -1, 1, 2] line_color = [255, 0, 0] # Red box = box.transpose(1, 2, 0) # from 4*520*520 to 520*520*3 for l in line_size: rr, cc = polygon_perimeter(contour[:, 0] + l, contour[:, 1] + l, shape=box.shape, clip=False) box[rr, cc, :3] = [line_color] # Safe the box with contour box = box.transpose(2, 0, 1) # from 520*520*3 to 3*520*520 with rasterio.open( f'{fp_processed}train_valid_test_contours/{roof_id}.png', 'w', **profile) as png_file: png_file.write(box) # Also save the cropped roof should we need it roof, _ = rasterio.mask.mask(box_file, [roof_geometry], crop=True) with rasterio.open( f'{fp_processed}train_valid_test_roofs/{roof_id}.png', 'w', **profile) as png_file: png_file.write(roof) # delete the aux.xml files os.remove( f'{fp_processed}train_valid_test_areas/{roof_id}.png.aux.xml' ) # Only delete after rasterio.open !!! os.remove( f'{fp_processed}train_valid_test_contours/{roof_id}.png.aux.xml' ) os.remove( f'{fp_processed}train_valid_test_roofs/{roof_id}.png.aux.xml' ) # create features area = roof_geometry.area complexity = 0 if type(roof_geometry) == Polygon: complexity = len(roof_geometry.exterior.coords.xy[0] ) - 1 # complexity of the shape elif type( roof_geometry ) == MultiPolygon: # take the complexity of the biggest shape max_area = 0 i = 0 for l in range(len(roof_geometry)): max_area = max(max_area, roof_geometry[l].area) if max_area == roof_geometry[l].area: i = l complexity = len( roof_geometry[i].exterior.coords.xy[0]) - 1 h = roof_geometry.centroid.x y = roof_geometry.centroid.y # Add the zonal_stats for the roof stats = [ 'min', 'max', 'median', 'count', 'majority', 'minority', 'unique', 'range', 'sum' ] zonal_stats = rasterstats.zonal_stats( roof_geometry, f'{fp}interim/stac/{country}/{place}/{place}_ortho-cog.tif', stats=stats, nodata=-999) # append roof features to csv file row = [ roof_id, country, place, roof_verified, area, complexity, h, y ] + [zonal_stats[0][k] for k in zonal_stats[0]] + [roof_label, is_test] with open(fp_processed + 'train_valid_test.csv', 'a') as csv_file: writer = csv.writer(csv_file) writer.writerow(row) print()
def rectangle_perimeter(r0, c0, width, height, shape=None, clip=False): rr, cc = [r0, r0 + width, r0 + width, r0], [c0, c0, c0 + height, c0 + height] return draw.polygon_perimeter(rr, cc, shape=shape, clip=clip)
def rsshow(self, n): """shows image with polygon region in white (color code 1)""" im = copy(self.ims[n]) spr, spc = polygon_perimeter(self.ser, self.sec) im[spr, spc] = 1 imshow(im)
m = get_face_blob(img_shape, x, y, s) p1 = (start[0], start[1]) p2 = (start[0] + extent[0], start[1]) p3 = (start[0] + extent[0], start[1] + extent[1]) p4 = (start[0], start[1] + extent[1]) face_boxes[str(face)].append([p1, p2, p3, p4]) #for skimage polygon r = np.array([p1[0], p2[0], p3[0], p4[0]]) c = np.array([p1[1], p2[1], p3[1], p4[1]]) #rr, cc = rectangle(start, extent=extent, shape=img_size) rr, cc = polygon_perimeter(r, c) #img[rr, cc] = 1 if face == whospeaks_now: curr_frame[cc - 1, rr - 1, :] = np.array([255, 0, 0]) curr_frame[cc, rr, :] = np.array([255, 0, 0]) curr_frame[cc + 1, rr + 1, :] = np.array([255, 0, 0]) speaker_map += m else: curr_frame[cc - 1, rr - 1, :] = np.array([0, 0, 255]) curr_frame[cc, rr, :] = np.array([0, 0, 255]) curr_frame[cc + 1, rr + 1, :] = np.array([0, 0, 255]) non_speaker_map += m vdata_boxes.append(np.expand_dims(curr_frame, axis=0))
def showSEvent(d, i, show=True): data = d[int(i), ...] max_eta = 5 max_phi = np.pi res = 30 neta = int(max_eta * res) nphi = int(max_phi * res) eeta = 2. * max_eta / float(neta) ephi = 2. * max_phi / float(nphi) def ieta(eta): return (eta + max_eta) / eeta def iphi(phi): return (phi + max_phi) / ephi image = np.zeros((neta, nphi, 5), dtype=np.uint8) for ip in range(data.shape[0]): p_data = data[ip, :] eta = p_data[0] phi = p_data[1] if eta == 0 and phi == 0: #print ip continue #pT = p_data[2] #lpT = min(max(np.log(pT)/5.,0.001), 10)*res/2. lpT = p_data[2] ptype = int(p_data[3]) layer = cc_layers[ptype] s = cc_shapes[ptype] R = lpT * res / 1. iee = ieta(eta) ip0 = iphi(phi) ip1 = iphi(phi + 2 * np.pi) ip2 = iphi(phi - 2 * np.pi) if s == 0: xi0, yi0 = draw.circle_perimeter(int(iee), int(ip0), radius=int(R), shape=image.shape[:2]) xi1, yi1 = draw.circle_perimeter(int(iee), int(ip1), radius=int(R), shape=image.shape[:2]) xi2, yi2 = draw.circle_perimeter(int(iee), int(ip2), radius=int(R), shape=image.shape[:2]) #if ptype == 5: # print "MET",eta,phi else: nv = s vx = [ iee + R * np.cos(ang) for ang in np.arange(0, 2 * np.pi, 2 * np.pi / nv) ] vy = [ ip0 + R * np.sin(ang) for ang in np.arange(0, 2 * np.pi, 2 * np.pi / nv) ] vy1 = [ ip1 + R * np.sin(ang) for ang in np.arange(0, 2 * np.pi, 2 * np.pi / nv) ] vy2 = [ ip2 + R * np.sin(ang) for ang in np.arange(0, 2 * np.pi, 2 * np.pi / nv) ] xi0, yi0 = draw.polygon_perimeter(vx, vy, shape=image.shape[:2]) xi1, yi1 = draw.polygon_perimeter(vx, vy1, shape=image.shape[:2]) xi2, yi2 = draw.polygon_perimeter(vx, vy2, shape=image.shape[:2]) xi = np.concatenate((xi0, xi1, xi2)) yi = np.concatenate((yi0, yi1, yi2)) image[xi, yi, layer] = 1 if show: fig = plt.figure(frameon=False) for i in range(image.shape[2]): image_show = image[:, :, i] plt.imshow(image_show.swapaxes(0, 1)) plt.axis('off') plt.show() return image
def rectangle_perimeter(x1, y1, width, height, shape=None, clip=False): rr, cc = [x1, x1 + width, x1 + width, x1], [y1, y1, y1 + height, y1 + height] return Draw.polygon_perimeter(rr, cc, shape=shape, clip=clip)
def unlock_screen(): base_folder = "./" path = os.path.abspath(base_folder) abs_path = path + "/" # Проверяем есть ли папки "Description_dataset" и "dataset" if os.path.exists(abs_path + "Description_dataset"): shutil.rmtree(abs_path + "Description_dataset", ignore_errors=True) if os.path.exists(abs_path + "dataset"): shutil.rmtree(abs_path + "dataset", ignore_errors=True) # Подключаемся к камере cam = cv2.VideoCapture( "/dev/video0") # Если путь отличается, МЕНЯЕМ ТУТ!!!! cam.set(3, 640) # установить ширину видео cam.set(4, 480) # установить высоту видео face_detector = cv2.CascadeClassifier( abs_path + 'haarcascade_frontalface_default.xml' ) # путь до Cascade, мы используем каскад Хаара по обнаружению лиц face_id = str(uuid.uuid4()) # Инициализация индивидуальной выборочной грани count = 0 while True: ret, img = cam.read() img = cv2.flip(img, 1) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faces = face_detector.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5, minSize=(20, 20)) for (x, y, w, h) in faces: cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2) count += 1 # Сохраните захваченное изображение в папку наборов данных # Создадим папку folder = "./dataset" if not os.path.exists(folder): os.makedirs(folder) cv2.imwrite( folder + "/" + str(face_id) + '_' + str(count) + ".jpg", gray[y:y + h, x:x + w]) k = cv2.waitKey(100) & 0xff # Нажмите «ESC» для выхода из видео if k == 27: break elif count >= 3: # Взять 3 образцов лица и остановить видео break cam.release() cv2.destroyAllWindows() # Извлекаем дескрипторы лица faces_folder_path = abs_path + "dataset" # Аргумент. Где ищем файлы .jpg(папка) detector = dlib.get_frontal_face_detector() sp = dlib.shape_predictor(abs_path + 'shape_predictor_68_face_landmarks.dat') facerec = dlib.face_recognition_model_v1( abs_path + 'dlib_face_recognition_resnet_model_v1.dat') for f in glob.glob(os.path.join(faces_folder_path + "*/", "*.jpg")): img = dlib.load_rgb_image(f) dets = detector(img, 2) # Теперь обработаем каждое лицо, которое мы нашли for k, d in enumerate(dets): polygon_perimeter( [d.top(), d.top(), d.bottom(), d.bottom()], [d.right(), d.left(), d.left(), d.right()]) # находим уникальные точки на лице изображения shape = sp(img, d) face_descriptor = facerec.compute_face_descriptor( img, shape ) # Вытаскиваем дескрипторы лица и сохроняем их в переменную face_descriptor # Сохраняем выделенные дескрипторы в разные файлы .pickle filename = str( uuid.uuid4() ) # даем нашему файлу .pickle уникальное имя с помощью библиотеке uuid # Создаем папку Description и в нее сохроняем файлы .pickle newpath = abs_path + "Description_dataset" if not os.path.exists( newpath): # Проверяем есть ли она в директории os.makedirs(newpath) # Создаем папку # Вытаскиваем из лица дескрипторы и сохроняем их в файл .pickle папку Description with open(abs_path + "Description_dataset/" + filename + '.pickle', 'wb') as file_save: pickle.dump( face_descriptor, file_save ) # pickle.dump - сохранение дескрипторов в двоичный файл .pickle find_file = os.listdir(abs_path + "Description_database/") # Где ищем файлы find_file_1 = random.choice(find_file) # Выбираем рандомно один файл face_rec_model_path = os.path.abspath( abs_path + "Description_database/" + find_file_1) # узнаем абсолютный путь find_file_2 = os.listdir(abs_path + "Description_dataset/") find_file_3 = random.choice(find_file_2) faces_folder_path = os.path.abspath(abs_path + "Description_dataset/" + find_file_3) # Сравниваем дискрипторы with open(face_rec_model_path, 'rb') as file_load: file_data_description_0 = pickle.load( file_load ) # pickle.load - загружаем из двоичного файла .pickle наш дискриптор with open( faces_folder_path, 'rb' ) as file_load_1: # Открываем на чтение все, что находится в переменной f и задаем все в новую переменную file_load_1 file_data_description_1 = pickle.load( file_load_1 ) # pickle.load - загружаем из всех файлов с расширением .pickle наши дискриптор a = distance.euclidean( file_data_description_0, file_data_description_1 ) # Рассчитываем Евклидово расстояние между двумя дексрипторами лиц f_1 = round( a, 2 ) # Округляем Евклидово расстояние между двумя дексрипторами лиц до двух знаков после запятой if 0 <= f_1 <= 0.45: # Если Евклидово расстояние между двумя дексрипторами лиц меньше или равно 0.45, то... os.system( "sudo loginctl unlock-sessions") # Разблокируем экран блокировки # Удаляем дискрипторы и фото лица shutil.rmtree(abs_path + "Description_dataset", ignore_errors=True) shutil.rmtree(abs_path + "dataset", ignore_errors=True) elif 0.46 <= f_1 <= 1: # Если Евклидово расстояние между двумя дексрипторами лиц больше 0.60, но меньше 1, выводим сообщение о не совпадении shutil.rmtree(abs_path + "Description_dataset", ignore_errors=True) shutil.rmtree(abs_path + "dataset", ignore_errors=True)
def simple_glacier_masks(gdir): """Compute glacier masks based on much simpler rules than the default. This is therefore more robust Parameters ---------- gdir : :py:class:`oggm.GlacierDirectory` where to write the data """ # open srtm tif-file: dem_dr = rasterio.open(gdir.get_filepath('dem'), 'r', driver='GTiff') dem = dem_dr.read(1).astype(rasterio.float32) # Grid nx = dem_dr.width ny = dem_dr.height assert nx == gdir.grid.nx assert ny == gdir.grid.ny # Correct the DEM (ASTER...) # Currently we just do a linear interp -- ASTER is totally shit anyway min_z = -999. isfinite = np.isfinite(dem) if (np.min(dem) <= min_z) or np.any(~isfinite): xx, yy = gdir.grid.ij_coordinates pnan = np.nonzero((dem <= min_z) | (~isfinite)) pok = np.nonzero((dem > min_z) | isfinite) points = np.array((np.ravel(yy[pok]), np.ravel(xx[pok]))).T inter = np.array((np.ravel(yy[pnan]), np.ravel(xx[pnan]))).T dem[pnan] = griddata(points, np.ravel(dem[pok]), inter) log.warning(gdir.rgi_id + ': DEM needed interpolation.') isfinite = np.isfinite(dem) if not np.all(isfinite): # see how many percent of the dem if np.sum(~isfinite) > (0.2 * nx * ny): raise RuntimeError('({}) too many NaNs in DEM'.format(gdir.rgi_id)) log.warning('({}) DEM needed zeros somewhere.'.format(gdir.rgi_id)) dem[isfinite] = 0 if np.min(dem) == np.max(dem): raise RuntimeError('({}) min equal max in the DEM.'.format( gdir.rgi_id)) # Proj if LooseVersion(rasterio.__version__) >= LooseVersion('1.0'): transf = dem_dr.transform else: transf = dem_dr.affine x0 = transf[2] # UL corner y0 = transf[5] # UL corner dx = transf[0] dy = transf[4] # Negative assert dx == -dy assert dx == gdir.grid.dx assert y0 == gdir.grid.corner_grid.y0 assert x0 == gdir.grid.corner_grid.x0 dem_dr.close() # Clip topography to 0 m a.s.l. dem = dem.clip(0) # Smooth DEM? if cfg.PARAMS['smooth_window'] > 0.: gsize = np.rint(cfg.PARAMS['smooth_window'] / dx) smoothed_dem = gaussian_blur(dem, np.int(gsize)) else: smoothed_dem = dem.copy() if not np.all(np.isfinite(smoothed_dem)): raise RuntimeError('({}) NaN in smoothed DEM'.format(gdir.rgi_id)) # Geometries outlines_file = gdir.get_filepath('outlines') geometry = gpd.GeoDataFrame.from_file(outlines_file).geometry[0] # Transform geometry into grid coordinates # It has to be in pix center coordinates because of how skimage works def proj(x, y): grid = gdir.grid.center_grid return grid.transform(x, y, crs=grid.proj) geometry = shapely.ops.transform(proj, geometry) # simple trick to correct invalid polys: # http://stackoverflow.com/questions/20833344/ # fix-invalid-polygon-python-shapely geometry = geometry.buffer(0) if not geometry.is_valid: raise RuntimeError('This glacier geometry is not valid.') # Compute the glacier mask (currently: center pixels + touched) nx, ny = gdir.grid.nx, gdir.grid.ny glacier_mask = np.zeros((ny, nx), dtype=np.uint8) glacier_ext = np.zeros((ny, nx), dtype=np.uint8) x, y = geometry.exterior.xy x, y = np.array(x), np.array(y) glacier_mask[skdraw.polygon(y, x)] = 1 glacier_mask[skdraw.polygon_perimeter(y, x)] = 1 glacier_ext[skdraw.polygon_perimeter(y, x)] = 1 for gint in geometry.interiors: x, y = gint.xy x, y = np.array(x), np.array(y) glacier_mask[skdraw.polygon(y, x)] = 0 glacier_mask[skdraw.polygon_perimeter(y, x)] = 0 # Because of the 0 values at nunataks boundaries, some "Ice Islands" # can happen within nunataks (e.g.: RGI40-11.00062) # See if we can filter them out easily regions, nregions = label(glacier_mask, structure=label_struct) if nregions > 1: log.debug('(%s) we had to cut an island in the mask', gdir.rgi_id) # Check the size of those region_sizes = [ np.sum(regions == r) for r in np.arange(1, nregions + 1) ] am = np.argmax(region_sizes) # Check not a strange glacier sr = region_sizes.pop(am) for ss in region_sizes: assert (ss / sr) < 0.1 glacier_mask[:] = 0 glacier_mask[np.where(regions == (am + 1))] = 1 # Last sanity check based on the masked dem tmp_max = np.max(dem[np.where(glacier_mask == 1)]) tmp_min = np.min(dem[np.where(glacier_mask == 1)]) if tmp_max < (tmp_min + 1): raise RuntimeError('({}) min equal max in the masked DEM.'.format( gdir.rgi_id)) # write out the grids in the netcdf file nc = gdir.create_gridded_ncdf_file('gridded_data') v = nc.createVariable('topo', 'f4', ( 'y', 'x', ), zlib=True) v.units = 'm' v.long_name = 'DEM topography' v[:] = dem v = nc.createVariable('topo_smoothed', 'f4', ( 'y', 'x', ), zlib=True) v.units = 'm' v.long_name = ('DEM topography smoothed' ' with radius: {:.1} m'.format(cfg.PARAMS['smooth_window'])) v[:] = smoothed_dem v = nc.createVariable('glacier_mask', 'i1', ( 'y', 'x', ), zlib=True) v.units = '-' v.long_name = 'Glacier mask' v[:] = glacier_mask v = nc.createVariable('glacier_ext', 'i1', ( 'y', 'x', ), zlib=True) v.units = '-' v.long_name = 'Glacier external boundaries' v[:] = glacier_ext # add some meta stats and close nc.max_h_dem = np.max(dem) nc.min_h_dem = np.min(dem) dem_on_g = dem[np.where(glacier_mask)] nc.max_h_glacier = np.max(dem_on_g) nc.min_h_glacier = np.min(dem_on_g) nc.close()
# Now let's run the detector over the images in the objects folder and display the # results. print("Showing detections on the images in the test folder...") win = dlib.image_window() for f in glob.glob(os.path.join(detector_folder, "detect/*.jpg")): print("Processing file: {}".format(f)) img = io.imread(f) dets = detector(img) print("Number of objects detected: {}".format(len(dets))) bOverLays = False for k, d in enumerate(dets): print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format( k, d.left(), d.top(), d.right(), d.bottom())) rr, cc = polygon_perimeter( [d.top(), d.top(), d.bottom(), d.bottom()], [d.right(), d.left(), d.left(), d.right()]) try: img[rr, cc] = (255, 0, 0) if bOverLays == False: bOverLays = True except: traceback.print_exc() # Save the image detections to a file for future review. if bOverLays == True: io.imsave(f.replace("detect/", "output/"), img) win.clear_overlay() win.set_image(img) win.add_overlay(dets) dlib.hit_enter_to_continue()
def main(): # np.set_printoptions(threshold=np.inf) # pc_file = '/home/lucerna/MEGA/project/AVP/2427439726050.npy' # pc = np.transpose(np.load(pc_file)) context_cloud = zmq.Context() socket_cloud = context_cloud.socket(zmq.SUB) socket_cloud.setsockopt(zmq.SUBSCRIBE, b"") print("Collecting point clouds...") socket_cloud.connect("tcp://localhost:5556") context_box = zmq.Context() socket_box = context_box.socket(zmq.SUB) socket_box.setsockopt(zmq.SUBSCRIBE, b"") print("Collecting bboxs...") socket_box.connect("tcp://localhost:5557") context_grid = zmq.Context() socket_grid = context_grid.socket(zmq.PUB) socket_grid.bind("tcp://*:5558") x_clip = np.array([0, 1.5]) y_clip = np.array([-1.5, 1.5]) z_clip = 0.1 grid_res = 100 object_res = 50 # create occupancy grid dim_x = int((x_clip[1] - x_clip[0]) * grid_res) dim_y = int((y_clip[1] - y_clip[0]) * grid_res) dim_x_object = int((x_clip[1] - x_clip[0]) * object_res) dim_y_object = int((y_clip[1] - y_clip[0]) * object_res) object_matrix = np.zeros([dim_x_object, dim_y_object]) car_matrix = np.zeros([dim_x_object, dim_y_object]) car_matrix_object = np.zeros([dim_x_object, dim_y_object]) pc_in = None bbox = None while True: if socket_cloud.poll(timeout=10) != 0: pc_in = np.transpose(recv_array(socket_cloud)) if socket_box.poll(timeout=10) != 0: bbox = recv_array(socket_box) occupancy_grid = OccupancyGrid(dim_x, dim_y) if bbox is not None: # add detection in the grid rect_x = np.zeros((4, )) rect_y = np.zeros((4, )) rect_x[0] = find_coords(bbox[0 + 8, 0], object_res) rect_y[0] = find_coords(bbox[0 + 8, 1], object_res, dim_y_object / 2) rect_x[1] = find_coords(bbox[4 + 8, 0], object_res) rect_y[1] = find_coords(bbox[4 + 8, 1], object_res, dim_y_object / 2) rect_x[2] = find_coords(bbox[6 + 8, 0], object_res) rect_y[2] = find_coords(bbox[6 + 8, 1], object_res, dim_y_object / 2) rect_x[3] = find_coords(bbox[2 + 8, 0], object_res) rect_y[3] = find_coords(bbox[2 + 8, 1], object_res, dim_y_object / 2) car_coords_x, car_coords_y = np.array( polygon(rect_x, rect_y, shape=(dim_x_object, dim_y_object))) car_matrix = np.zeros([dim_x_object, dim_y_object]) car_matrix_object = np.zeros([dim_x_object, dim_y_object]) car_matrix[car_coords_x, car_coords_y] = 1 rect_x[0] = find_coords(bbox[0, 0], grid_res) rect_y[0] = find_coords(bbox[0, 1], grid_res, dim_y / 2) rect_x[1] = find_coords(bbox[4, 0], grid_res) rect_y[1] = find_coords(bbox[4, 1], grid_res, dim_y / 2) rect_x[2] = find_coords(bbox[6, 0], grid_res) rect_y[2] = find_coords(bbox[6, 1], grid_res, dim_y / 2) rect_x[3] = find_coords(bbox[2, 0], grid_res) rect_y[3] = find_coords(bbox[2, 1], grid_res, dim_y / 2) car_peri_coords_x, car_peri_coords_y = np.array( polygon_perimeter(rect_x, rect_y, shape=(dim_x, dim_y), clip=True)) if pc_in is not None: # add objects in the grid pc = pc_in[np.where((pc_in[:, 1] > y_clip[0]) & (pc_in[:, 1] < y_clip[1]))] pc[:, 0] -= np.min(pc[:, 0]) pc = pc[np.where(pc[:, 0] < x_clip[1])] pc[:, 2] += -z_clip pc = pc[np.where((pc[:, 2] > 0))] pc = pc[np.where((pc[:, 3] > 100))] pc_grid = pc[:, 0:2] pc_grid[:, 0] = (np.floor(pc_grid[:, 0] * object_res)) pc_grid[:, 1] = (np.floor(pc_grid[:, 1] * object_res) + dim_y_object / 2) pc_grid = pc_grid.astype(int) pc_grid, counts = np.unique(pc_grid, return_counts=True, axis=0) pc_grid = pc_grid[np.where(counts > grid_res / object_res)] object_matrix = np.zeros([dim_x_object, dim_y_object]) object_matrix[pc_grid[:, 0], pc_grid[:, 1]] = 1 # inflate the object matrix # for ind in range(pc_grid.shape[0]): # neighbors = find_neighbours(pc_grid[ind, 0], pc_grid[ind, 1], dim_x_object, dim_y_object, option = 0) # for neighbor in neighbors: # object_matrix[neighbor[0], neighbor[1]] = 1 # pc_grid = np.transpose(np.array(np.where(object_matrix == 1))) for ind in range(pc_grid.shape[0]): if car_matrix[pc_grid[ind, 0], pc_grid[ind, 1]] == 1: car_matrix_object[pc_grid[ind, 0], pc_grid[ind, 1]] = 1 else: neighbors = find_neighbours(pc_grid[ind, 0], pc_grid[ind, 1], dim_x_object, dim_y_object, option=0) for neighbor in neighbors: if car_matrix[neighbor[0], neighbor[1]] == 1: car_matrix_object[pc_grid[ind, 0], pc_grid[ind, 1]] = 1 continue object_matrix = rescale(object_matrix, grid_res / object_res, anti_aliasing=False) object_matrix[np.where(object_matrix > 0)] = 1 car_matrix_object = rescale(car_matrix_object, grid_res / object_res, anti_aliasing=False) car_matrix_object[np.where(car_matrix_object > 0)] = 1 matrix_labels, num = label(object_matrix, connectivity=2, return_num=True) find_flag = 0 for num_ind in range(num + 1): label_xy = np.array(np.where(matrix_labels == num_ind)) if num_ind != 0: for ind in range(label_xy.shape[1]): if car_matrix_object[label_xy[0, ind], label_xy[1, ind]] == 1: car_matrix_object[label_xy[0, :], label_xy[1, :]] = 1 find_flag = 1 if find_flag == 1: break # print(label_xy.shape[1], ind) if pc_in is not None and bbox is not None: occupancy_grid.matrix[np.where(object_matrix > 0)] = 1 occupancy_grid.matrix[np.where(car_matrix_object > 0)] = 2 occupancy_grid.matrix[car_peri_coords_x, car_peri_coords_y] = 3 # perimeter line occupancy_grid.update_image() send_array(socket_grid, occupancy_grid.image)
def sanitize_page(self, page, page_id): LOG = getLogger('processor.RepairSegmentation') regions = page.get_AllRegions(classes=['Text']) page_image, page_coords, _ = self.workspace.image_from_page( page, page_id) for region in regions: LOG.info('Sanitizing region "%s"', region.id) lines = region.get_TextLine() if not lines: LOG.warning('Page "%s" region "%s" contains no textlines', page_id, region.id) continue heights = [] tops = [] # get labels: region_mask = np.zeros((page_image.height, page_image.width), dtype=np.uint8) for line in lines: line_polygon = coordinates_of_segment(line, page_image, page_coords) line_xywh = xywh_from_polygon(line_polygon) heights.append(line_xywh['h']) tops.append(line_xywh['y']) region_mask[draw.polygon(line_polygon[:, 1], line_polygon[:, 0], region_mask.shape)] = 1 region_mask[draw.polygon_perimeter(line_polygon[:, 1], line_polygon[:, 0], region_mask.shape)] = 1 # estimate scale: heights = np.array(heights) scale = int(np.max(heights)) tops = np.array(tops) order = np.argsort(tops) heights = heights[order] tops = tops[order] if len(lines) > 1: # if interline spacing is larger than line height, use this bottoms = tops + heights deltas = tops[1:] - bottoms[:-1] scale = max(scale, int(np.max(deltas))) # close labels: region_mask = np.pad(region_mask, scale) # protect edges region_mask = np.array(morphology.binary_closing( region_mask, np.ones((scale, 1))), dtype=np.uint8) region_mask = region_mask[scale:-scale, scale:-scale] # unprotect # extend margins (to ensure simplified hull polygon is outside children): region_mask = filters.maximum_filter(region_mask, 3) # 1px in each direction # find outer contour (parts): contours, _ = cv2.findContours(region_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # determine areas of parts: areas = [cv2.contourArea(contour) for contour in contours] total_area = sum(areas) if not total_area: # ignore if too small LOG.warning('Zero contour area in region "%s"', region.id) continue # pick contour and convert to absolute: region_polygon = None for i, contour in enumerate(contours): area = areas[i] if area / total_area < 0.1: LOG.warning( 'Ignoring contour %d too small (%d/%d) in region "%s"', i, area, total_area, region.id) continue # simplify shape (until valid): # can produce invalid (self-intersecting) polygons: #polygon = cv2.approxPolyDP(contour, 2, False)[:, 0, ::] # already ordered x,y polygon = contour[:, 0, ::] # already ordered x,y polygon = Polygon(polygon).simplify(1) polygon = make_valid(polygon) polygon = polygon.exterior.coords[:-1] # keep open if len(polygon) < 4: LOG.warning( 'Ignoring contour %d less than 4 points in region "%s"', i, region.id) continue if region_polygon is not None: LOG.error( 'Skipping region "%s" due to non-contiguous contours', region.id) region_polygon = None break region_polygon = coordinates_for_segment( polygon, page_image, page_coords) if region_polygon is not None: LOG.info('Using new coordinates for region "%s"', region.id) region.get_Coords().set_points( points_from_polygon(region_polygon))
def performDetect(imagePath="data/dog.jpg", thresh= 0.25, configPath = "./cfg/yolov4.cfg", weightPath = "yolov4.weights", metaPath= "./cfg/coco.data", showImage= True, makeImageOnly = False, initOnly= False, mask_present_label = True, mask_path = '/content/drive/My Drive/equalaf4.pth'): """ Convenience function to handle the detection and returns of objects. Displaying bounding boxes requires libraries scikit-image and numpy Parameters ---------------- imagePath: str Path to the image to evaluate. Raises ValueError if not found thresh: float (default= 0.25) The detection threshold configPath: str Path to the configuration file. Raises ValueError if not found weightPath: str Path to the weights file. Raises ValueError if not found metaPath: str Path to the data file. Raises ValueError if not found showImage: bool (default= True) Compute (and show) bounding boxes. Changes return. makeImageOnly: bool (default= False) If showImage is True, this won't actually *show* the image, but will create the array and return it. initOnly: bool (default= False) Only initialize globals. Don't actually run a prediction. Returns ---------------------- When showImage is False, list of tuples like ('obj_label', confidence, (bounding_box_x_px, bounding_box_y_px, bounding_box_width_px, bounding_box_height_px)) The X and Y coordinates are from the center of the bounding box. Subtract half the width or height to get the lower corner. Otherwise, a dict with { "detections": as above "image": a numpy array representing an image, compatible with scikit-image "caption": an image caption } """ # Import the global variables. This lets us instance Darknet once, then just call performDetect() again without instancing again global metaMain, netMain, altNames #pylint: disable=W0603 assert 0 < thresh < 1, "Threshold should be a float between zero and one (non-inclusive)" if not os.path.exists(configPath): raise ValueError("Invalid config path `"+os.path.abspath(configPath)+"`") if not os.path.exists(weightPath): raise ValueError("Invalid weight path `"+os.path.abspath(weightPath)+"`") if not os.path.exists(metaPath): raise ValueError("Invalid data file path `"+os.path.abspath(metaPath)+"`") if netMain is None: netMain = load_net_custom(configPath.encode("ascii"), weightPath.encode("ascii"), 0, 1) # batch size = 1 if metaMain is None: metaMain = load_meta(metaPath.encode("ascii")) if altNames is None: # In Python 3, the metafile default access craps out on Windows (but not Linux) # Read the names file and create a list to feed to detect try: with open(metaPath) as metaFH: metaContents = metaFH.read() import re match = re.search("names *= *(.*)$", metaContents, re.IGNORECASE | re.MULTILINE) if match: result = match.group(1) else: result = None try: if os.path.exists(result): with open(result) as namesFH: namesList = namesFH.read().strip().split("\n") altNames = [x.strip() for x in namesList] except TypeError: pass except Exception: pass if initOnly: print("Initialized detector") return None if not os.path.exists(imagePath): raise ValueError("Invalid image path `"+os.path.abspath(imagePath)+"`") # Do the detection print("detect_image called") #detections = detect(netMain, metaMain, imagePath, thresh) # if is used cv2.imread(image) detections = detect(netMain, metaMain, imagePath.encode("ascii"), thresh) ################################# import pandas as pd load_mask_wt(mask_path) mask_model.eval() BATCH_SIZE = 0 predic = [] df = pd.DataFrame(columns=['name', 'x1', 'x2', 'y1', 'y2', 'classname']) ################################# if showImage: try: from skimage import io, draw import numpy as np image = io.imread(imagePath) print("*** "+str(len(detections))+" Results, color coded by confidence ***") imcaption = [] result = [] for detection in detections: label = detection[0] confidence = detection[1] pstring = label+": "+str(np.rint(100 * confidence))+"%" imcaption.append(pstring) print(BATCH_SIZE) print(pstring) bounds = detection[2] shape = image.shape # x = shape[1] # xExtent = int(x * bounds[2] / 100) # y = shape[0] # yExtent = int(y * bounds[3] / 100) yExtent = int(bounds[3]) xEntent = int(bounds[2]) # Coordinates are around the center xCoord = int(bounds[0] - bounds[2]/2) yCoord = int(bounds[1] - bounds[3]/2) #changed ########################################### font_scale = 0.35 thickness = 1 blue = (0,0,255) green = (0,255,0) red = (255,0,0) font=cv2.FONT_HERSHEY_COMPLEX x, y, w, h = xCoord, yCoord, int(bounds[2]), int(bounds[3]) print(x, y, w, h) if(x<0): x=0 if(y<0): y=0 detect_mask_img = image detect_mask_img = detect_mask_img[y:y+h, x:x+w] #pil_image = Image.fromarray(detect_mask_img, mode = "RGB") #pil_image = train_transforms(pil_image) #img = pil_image.unsqueeze(0) result.append(detect_mask_img) BATCH_SIZE += 1 predic.append(0) #--------------------------------------------- comp = Image_Dataset(result, transform=train_transforms) test_loader = torch.utils.data.DataLoader(comp, batch_size=BATCH_SIZE, shuffle=False) print("accessing mask model") prediction_list = [] with torch.no_grad(): print("load tl") for X in test_loader: #X = X.cuda() print("make prediction") result = mask_model(X) _, maximum = torch.max(result.data, 1) print(maximum.tolist()) prediction_list = maximum.tolist() print("predictions: ", prediction_list) #---------------------------------------------------- i=0 for detection in detections: label = detection[0] confidence = detection[1] pstring = label+": "+str(np.rint(100 * confidence))+"%" imcaption.append(pstring) print(pstring) bounds = detection[2] shape = image.shape # x = shape[1] # xExtent = int(x * bounds[2] / 100) # y = shape[0] # yExtent = int(y * bounds[3] / 100) yExtent = int(bounds[3]) xEntent = int(bounds[2]) # Coordinates are around the center xCoord = int(bounds[0] - bounds[2]/2) yCoord = int(bounds[1] - bounds[3]/2) x, y, w, h = xCoord, yCoord, int(bounds[2]), int(bounds[3]) prediction = prediction_list[i] print("processing prediction") if prediction == 0: if mask_present_label == True: cv2.putText(image, "No Mask", (x,y - 10), font, font_scale, red, thickness) print("Label print", mask_present_label) else: print("Label print", mask_present_label) print("No mask") boxColor = red elif prediction == 1: if mask_present_label == True: cv2.putText(image, "Masked", (x,y - 10), font, font_scale, green, thickness) print("Label print", mask_present_label) else: print("Label print", mask_present_label) print("Mask") boxColor = green i+=1 op = { "name": imagePath.split('/')[-1], "x1": x, "x2" : (x+w), "y1" : y, "y2" : (y+h), "classname" : prediction } op = pd.Series(op) df = df.append(op, ignore_index=True) #################################################### boundingBox = [ [xCoord, yCoord], [xCoord, yCoord + yExtent], [xCoord + xEntent, yCoord + yExtent], [xCoord + xEntent, yCoord] ] # Wiggle it around to make a 3px border rr, cc = draw.polygon_perimeter([x[1] for x in boundingBox], [x[0] for x in boundingBox], shape= shape) rr2, cc2 = draw.polygon_perimeter([x[1] + 1 for x in boundingBox], [x[0] for x in boundingBox], shape= shape) rr3, cc3 = draw.polygon_perimeter([x[1] - 1 for x in boundingBox], [x[0] for x in boundingBox], shape= shape) rr4, cc4 = draw.polygon_perimeter([x[1] for x in boundingBox], [x[0] + 1 for x in boundingBox], shape= shape) rr5, cc5 = draw.polygon_perimeter([x[1] for x in boundingBox], [x[0] - 1 for x in boundingBox], shape= shape) #boxColor = (int(255 * (1 - (confidence ** 2))), int(255 * (confidence ** 2)), 0) draw.set_color(image, (rr, cc), boxColor, alpha= 0.8) draw.set_color(image, (rr2, cc2), boxColor, alpha= 0.8) draw.set_color(image, (rr3, cc3), boxColor, alpha= 0.8) draw.set_color(image, (rr4, cc4), boxColor, alpha= 0.8) draw.set_color(image, (rr5, cc5), boxColor, alpha= 0.8) if not makeImageOnly: io.imsave(fname="/content/drive/My Drive/result.jpg", arr=image) io.imshow(image) io.show() '''detections = { "detections": detections, "image": image, "caption": "\n<br/>".join(imcaption) }''' except Exception as e: print("Unable to show image: "+str(e)) return df
import numpy as np from skimage import io, draw detector = dlib.get_frontal_face_detector() for f in sys.argv[1:]: print("Processing file: {}".format(f)) img = io.imread(f) dets = detector(img, 1) print("Number of faces detected: {}".format(len(dets))) for i, detect in enumerate(dets): print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format( i, detect.left(), detect.top(), detect.right(), detect.bottom())) Y = np.array([detect.top(), detect.top(), detect.bottom(), detect.bottom()]) X = np.array([detect.left(), detect.right(), detect.right(), detect.left()]) rr, cc = draw.polygon_perimeter(Y, X) draw.set_color(img, [rr, cc], [255, 0, 0]) io.imsave('detector.jpg', img) dlib.hit_enter_to_continue() # 检测人脸得分和识别不同方向的人脸 if (len(sys.argv[1:]) > 0): img = io.imread(sys.argv[1]) dets, scores, idx = detector.run(img, 1, -1) for i, d in enumerate(dets): print("Detection {}, score: {}, face_type:{}".format( d, scores[i], idx[i]))
def imagemarking(image, detections): try: from skimage import io, draw import numpy as np # print("*** "+str(len(detections))+" Results, color coded by confidence ***") imcaption = [] for detection in detections: label = detection[0] confidence = detection[1] pstring = label + ": " + str(np.rint(100 * confidence)) + "%" imcaption.append(pstring) # print(pstring) bounds = detection[2] shape = image.shape # x = shape[1] # xExtent = int(x * bounds[2] / 100) # y = shape[0] # yExtent = int(y * bounds[3] / 100) yExtent = int(bounds[3]) xEntent = int(bounds[2]) # Coordinates are around the center xCoord = int(bounds[0] - bounds[2] / 2) yCoord = int(bounds[1] - bounds[3] / 2) boundingBox = [[xCoord, yCoord], [xCoord, yCoord + yExtent], [xCoord + xEntent, yCoord + yExtent], [xCoord + xEntent, yCoord]] # Wiggle it around to make a 3px border rr, cc = draw.polygon_perimeter([x[1] for x in boundingBox], [x[0] for x in boundingBox], shape=shape) rr2, cc2 = draw.polygon_perimeter( [x[1] + 1 for x in boundingBox], [x[0] for x in boundingBox], shape=shape) rr3, cc3 = draw.polygon_perimeter( [x[1] - 1 for x in boundingBox], [x[0] for x in boundingBox], shape=shape) rr4, cc4 = draw.polygon_perimeter( [x[1] for x in boundingBox], [x[0] + 1 for x in boundingBox], shape=shape) rr5, cc5 = draw.polygon_perimeter( [x[1] for x in boundingBox], [x[0] - 1 for x in boundingBox], shape=shape) boxColor = (int(255 * (1 - (confidence**2))), int(255 * (confidence**2)), 0) draw.set_color(image, (rr, cc), boxColor, alpha=0.8) draw.set_color(image, (rr2, cc2), boxColor, alpha=0.8) draw.set_color(image, (rr3, cc3), boxColor, alpha=0.8) draw.set_color(image, (rr4, cc4), boxColor, alpha=0.8) draw.set_color(image, (rr5, cc5), boxColor, alpha=0.8) io.imshow(image) io.show() # detections = { # "detections": detections, # "image": image, # "caption": "\n<br/>".join(imcaption) # } except Exception as e: print("Unable to show image: " + str(e))
def performDetect(imagePath="data/dog.jpg", thresh= 0.25, configPath = "./cfg/yolov3.cfg", weightPath = "yolov3.weights", metaPath= "./data/coco.data", showImage= True, makeImageOnly = False, initOnly= False): """ Convenience function to handle the detection and returns of objects. Displaying bounding boxes requires libraries scikit-image and numpy Parameters ---------------- imagePath: str Path to the image to evaluate. Raises ValueError if not found thresh: float (default= 0.25) The detection threshold configPath: str Path to the configuration file. Raises ValueError if not found weightPath: str Path to the weights file. Raises ValueError if not found metaPath: str Path to the data file. Raises ValueError if not found showImage: bool (default= True) Compute (and show) bounding boxes. Changes return. makeImageOnly: bool (default= False) If showImage is True, this won't actually *show* the image, but will create the array and return it. initOnly: bool (default= False) Only initialize globals. Don't actually run a prediction. Returns ---------------------- When showImage is False, list of tuples like ('obj_label', confidence, (bounding_box_x_px, bounding_box_y_px, bounding_box_width_px, bounding_box_height_px)) The X and Y coordinates are from the center of the bounding box. Subtract half the width or height to get the lower corner. Otherwise, a dict with { "detections": as above "image": a numpy array representing an image, compatible with scikit-image "caption": an image caption } """ # Import the global variables. This lets us instance Darknet once, then just call performDetect() again without instancing again global metaMain, netMain, altNames #pylint: disable=W0603 assert 0 < thresh < 1, "Threshold should be a float between zero and one (non-inclusive)" if not os.path.exists(configPath): raise ValueError("Invalid config path `"+os.path.abspath(configPath)+"`") if not os.path.exists(weightPath): raise ValueError("Invalid weight path `"+os.path.abspath(weightPath)+"`") if not os.path.exists(metaPath): raise ValueError("Invalid data file path `"+os.path.abspath(metaPath)+"`") if netMain is None: netMain = load_net_custom(configPath.encode("ascii"), weightPath.encode("ascii"), 0, 1) # batch size = 1 if metaMain is None: metaMain = load_meta(metaPath.encode("ascii")) if altNames is None: # In Python 3, the metafile default access craps out on Windows (but not Linux) # Read the names file and create a list to feed to detect try: with open(metaPath) as metaFH: metaContents = metaFH.read() import re match = re.search("names *= *(.*)$", metaContents, re.IGNORECASE | re.MULTILINE) if match: result = match.group(1) else: result = None try: if os.path.exists(result): with open(result) as namesFH: namesList = namesFH.read().strip().split("\n") altNames = [x.strip() for x in namesList] except TypeError: pass except Exception: pass if initOnly: print("Initialized detector") return None if not os.path.exists(imagePath): raise ValueError("Invalid image path `"+os.path.abspath(imagePath)+"`") # Do the detection detections = detect(netMain, metaMain, imagePath.encode("ascii"), thresh) if showImage: try: from skimage import io, draw import numpy as np image = io.imread(imagePath) print("*** "+str(len(detections))+" Results, color coded by confidence ***") imcaption = [] for detection in detections: label = detection[0] confidence = detection[1] pstring = label+": "+str(np.rint(100 * confidence))+"%" imcaption.append(pstring) print(pstring) bounds = detection[2] shape = image.shape # x = shape[1] # xExtent = int(x * bounds[2] / 100) # y = shape[0] # yExtent = int(y * bounds[3] / 100) yExtent = int(bounds[3]) xEntent = int(bounds[2]) # Coordinates are around the center xCoord = int(bounds[0] - bounds[2]/2) yCoord = int(bounds[1] - bounds[3]/2) boundingBox = [ [xCoord, yCoord], [xCoord, yCoord + yExtent], [xCoord + xEntent, yCoord + yExtent], [xCoord + xEntent, yCoord] ] # Wiggle it around to make a 3px border rr, cc = draw.polygon_perimeter([x[1] for x in boundingBox], [x[0] for x in boundingBox], shape= shape) rr2, cc2 = draw.polygon_perimeter([x[1] + 1 for x in boundingBox], [x[0] for x in boundingBox], shape= shape) rr3, cc3 = draw.polygon_perimeter([x[1] - 1 for x in boundingBox], [x[0] for x in boundingBox], shape= shape) rr4, cc4 = draw.polygon_perimeter([x[1] for x in boundingBox], [x[0] + 1 for x in boundingBox], shape= shape) rr5, cc5 = draw.polygon_perimeter([x[1] for x in boundingBox], [x[0] - 1 for x in boundingBox], shape= shape) boxColor = (int(255 * (1 - (confidence ** 2))), int(255 * (confidence ** 2)), 0) draw.set_color(image, (rr, cc), boxColor, alpha= 0.8) draw.set_color(image, (rr2, cc2), boxColor, alpha= 0.8) draw.set_color(image, (rr3, cc3), boxColor, alpha= 0.8) draw.set_color(image, (rr4, cc4), boxColor, alpha= 0.8) draw.set_color(image, (rr5, cc5), boxColor, alpha= 0.8) if not makeImageOnly: io.imshow(image) io.show() detections = { "detections": detections, "image": image, "caption": "\n<br/>".join(imcaption) } except Exception as e: print("Unable to show image: "+str(e)) return detections
def draw_path(rs, cs, shp, lw): rr, cc = draw.polygon_perimeter(rs, cs, shp, True) return draw_thick(rr, cc, lw, shp)
def hex_mask(image, x0, y0, r_aper, arr_size, plot = False): mask_size = 100 x_cen, y_cen, img = fiber_centers(r_aper, arr_size, mask_size) pixel_list = {} #If only looking for one fiber this will output in a slightly different format. if arr_size == 1: #Calculating the vertices and rounding down so there will not be holes in the pixels later on. x = x_cen['1'] y = y_cen['1'] height = np.floor(r_aper * np.cos( np.pi/6)) width = np.trunc(r_aper * np.sin( np.pi/6)) vertex_x, vertex_y = x + width, y + height #Defining the vertices here and then inputting it into skimage's polygon and polygon perimeter. c = np.array([ x - r_aper , (2 * x) - vertex_x , vertex_x , x + r_aper +1, vertex_x +1 , (2 * x) - vertex_x , x - r_aper ]) r = np.array([ y , vertex_y , vertex_y + 1, y, (2 * y) - vertex_y , (2 * y) - vertex_y , y ]) yy, xx = polygon_perimeter(r,c) rr, cc = polygon(yy, xx) #Saving all the points into a dictionary pixel_list['x/y'][str(fiber_number)] #rr = np.append([rr],[yy]) #cc = np.append([cc],[xx]) pixel_list['x'] = cc pixel_list['y'] = rr img[rr, cc] = 1 if plot == False: return pixel_list, x_cen, y_cen, if plot == True: fig, ax = plt.subplots() ax.scatter(rr , cc ) #For cases in which number of fibers is greater than 1 if arr_size > 1: pixel_list['x'] = {} pixel_list['y'] = {} for i in range(0, arr_size): #Calculating the vertices and rounding down so there will not be holes in the pixels later on. x = x_cen[str(i + 1)] y = y_cen[str(i + 1)] height = np.floor(r_aper * np.cos( np.pi/6)) width = np.floor(r_aper * np.sin( np.pi/6)) vertex_x, vertex_y = (x + width), (y + height) #Defining the vertices here and then inputting it into skimage's polygon and polygon perimeter. c = np.array([ x - r_aper, (2 * x) - vertex_x , vertex_x, x + r_aper + 1 , vertex_x + 1, (2 * x) - vertex_x , x - r_aper]) r = np.array([ y , vertex_y, vertex_y + 1, y, (2 * y) - vertex_y , (2 * y) - vertex_y , y]) yy, xx = polygon_perimeter(r, c) rr, cc = polygon(yy, xx) #Saving all the points into a dictionary pixel_list['x/y'][str(fiber_number)] #For the bin mask we add 1 to the previous mask so we can find the overlap later on. #rr = np.append([rr],[yy]) #cc = np.append([cc],[xx]) pixel_list['x'][str(i + 1)] = cc pixel_list['y'][str(i + 1)] = rr img[rr,cc] = img[rr,cc] + 1 #Now we want to find the overlap in which the bin mask bits are greater than 1 overlap = np.where(img > 1) for i in range(0, len(overlap[0])): overlap_difference = [] for j in range(0, arr_size): delete = np.where(np.logical_and(pixel_list['y'][str(j + 1)] == overlap[0][i], pixel_list['x'][str(j + 1)] == overlap[1][i])) difference = ((x_cen[str(j + 1)] - overlap[1][i]) ** 2) + ((y_cen[str(j + 1)] - overlap[1][i]) **2) if len(delete[0]) > 0: pixel_list['y'][str(j+1)] = np.delete(pixel_list['y'][str(j+1)], delete[0]) pixel_list['x'][str(j+1)] = np.delete(pixel_list['x'][str(j+1)], delete[0]) for k in range(0, arr_size): difference = ((x_cen[str(k + 1)] - overlap[1][i]) ** 2) + ((y_cen[str(k + 1)] - overlap[0][i]) **2) overlap_difference.append(difference) assignment = [e for e,x in enumerate(overlap_difference) if x == np.min(overlap_difference)] if len(assignment) == 1: assignment = assignment[0] + 1 pixel_list['y'][str(assignment)] = np.append(pixel_list['y'][str(assignment)], overlap[0][i]) pixel_list['x'][str(assignment)] = np.append(pixel_list['x'][str(assignment)], overlap[1][i]) img[(pixel_list['y'][str(assignment)][-1], pixel_list['x'][str(assignment)][-1])] = 1 elif len(assignment) > 1: if assignment[0] == 2 or assignment[0] == 5: assignment = assignment[0] + 1 elif assignment[1] == 2 or assignment[1] == 5: assignment = assignment[1] + 1 #elif len(pixel_list['x'][str(assignment[0] + 1)]) > len(pixel_list['x'][str(assignment[1] + 1)]): # assignment = assignment[1] + 1 #elif len(pixel_list['x'][str(assignment[0] + 1)]) < len(pixel_list['x'][str(assignment[1] + 1)]): # assignment = assignment[0] + 1 #elif len(pixel_list['x'][str(assignment[0] + 1)]) == len(pixel_list['x'][str(assignment[1] + 1)]): else: assignment = assignment[0] + 1 pixel_list['y'][str(assignment)] = np.append(pixel_list['y'][str(assignment)], overlap[0][i]) pixel_list['x'][str(assignment)] = np.append(pixel_list['x'][str(assignment)], overlap[1][i]) img[(pixel_list['y'][str(assignment)][-1], pixel_list['x'][str(assignment)][-1])] = 1 #Calculating where the nearest fiber is based on the distance from the overlapping tuple to the center of the other fibers. #for j in range(0, arr_size): # difference = ((overlap[0][i] - (y_cen[str(j + 1)])) ** 2) + ((overlap[1][i] - (x_cen[str(j + 1)])) ** 2) # overlap_difference.append(difference) #Trying to find the minimum difference (where the objects are closest). #Factoring in pixels that might be equidistant to other objects. #Assigning pixels to the fiber with fewer pixels and then assigning to the smaller fiber_number #We want to have more flux in the central fibers and this should account for that in those cases. # assignment = [e for e,x in enumerate(overlap_difference) if x == np.min(overlap_difference)] # if len(assignment) == 1: # assignment = assignment[0] + 1 # elif len(assignment) > 1: # if len(assignment) > 2: # print(assignment) # if len(pixel_list['x'][str(assignment[0] + 1)]) > len(pixel_list['x'][str(assignment[1] + 1)]): # assignment = assignment[1] + 1 # elif len(pixel_list['x'][str(assignment[0] + 1)]) < len(pixel_list['x'][str(assignment[1] + 1)]): # assignment = assignment[0] + 1 # elif len(pixel_list['x'][str(assignment[0] + 1)]) == len(pixel_list['x'][str(assignment[1] + 1)]): # if assignment[0] < assignment[1]: # assignment = assignment[0] + 1 # else: # assignment = assignment[1] + 1 #Going into each fiber to delete the specific x,y tuple to remove the potential overlap #Then adding the pixel back into the correct fiber number # for k in range(0, arr_size): # delete = np.where(np.logical_and(pixel_list['y'][str(k + 1)] == overlap[0][i], pixel_list['x'][str(k + 1)] == overlap[1][i])) # if len(delete[0]) > 0: # pixel_list['y'][str(k+1)] = np.delete(pixel_list['y'][str(k+1)], delete[0]) # pixel_list['x'][str(k+1)] = np.delete(pixel_list['x'][str(k+1)], delete[0]) # pixel_list['y'][str(assignment)] = np.append(pixel_list['y'][str(assignment)], (overlap[0][i] - 50 + y_cen[str(assignment)])) # pixel_list['x'][str(assignment)] = np.append(pixel_list['x'][str(assignment)], (overlap[1][i] - 50 + x_cen[str(assignment)])) # img[(pixel_list['y'][str(assignment)][-1] + 50 - y_cen[str(assignment)]),(pixel_list['x'][str(assignment)][-1] + 50 - x_cen[str(assignment)])] = 1 #Plot if true and return the pixels if plot == False: return pixel_list, x_cen, y_cen, if plot == True: fig, ax = plt.subplots() for i in range(0, arr_size): ax.scatter(pixel_list['y'][str(i + 1)] , pixel_list['x'][str(i + 1)]) #Image shapes and sizes might be too much so we'll only plot a smaller box around them. ax.set_aspect('equal', 'datalim') ax.imshow(image[(y0 - 50):(y0 + 50), (x0 - 50): (x0 + 50)], cmap = 'gray') ax.set_xlim(35, 65) ax.set_ylim(35, 65) ax.invert_yaxis() plt.show() if arr_size > 1: for i in range(0, arr_size): pixel_list['x'][str(i + 1)] += (x0 - 50) pixel_list['y'][str(i + 1)] += (y0 - 50) if arr_size == 1: pixel_list['x'] += (x0 - 50) pixel_list['y'] += (y0 - 50) return pixel_list, x_cen, y_cen, img, yy, xx
def perform_detect( self, image_path_or_buf='data/dog.jpg', thresh: float = 0.25, show_image: bool = True, make_image_only: bool = False, ): self.lock.acquire() assert 0 < thresh < 1, 'Threshold should be a float between zero and one (non-inclusive)' detections = self.detect(image_path_or_buf, thresh) if show_image and isinstance(image_path_or_buf, str): try: from skimage import io, draw image = io.imread(image_path_or_buf) print('*** ' + str(len(detections)) + ' Results, color coded by confidence ***') imcaption = [] for detection in detections: label = detection[0] confidence = detection[1] pstring = label + ': ' + str(np.rint( 100 * confidence)) + '%' imcaption.append(pstring) print(pstring) bounds = detection[2] shape = image.shape # x = shape[1] # xExtent = int(x * bounds[2] / 100) # y = shape[0] # yExtent = int(y * bounds[3] / 100) yExtent = int(bounds[3]) xEntent = int(bounds[2]) # Coordinates are around the center xCoord = int(bounds[0] - bounds[2] / 2) yCoord = int(bounds[1] - bounds[3] / 2) boundingBox = [[xCoord, yCoord], [xCoord, yCoord + yExtent], [xCoord + xEntent, yCoord + yExtent], [xCoord + xEntent, yCoord]] # Wiggle it around to make a 3px border rr, cc = draw.polygon_perimeter( [x[1] for x in boundingBox], [x[0] for x in boundingBox], shape=shape) rr2, cc2 = draw.polygon_perimeter( [x[1] + 1 for x in boundingBox], [x[0] for x in boundingBox], shape=shape) rr3, cc3 = draw.polygon_perimeter( [x[1] - 1 for x in boundingBox], [x[0] for x in boundingBox], shape=shape) rr4, cc4 = draw.polygon_perimeter( [x[1] for x in boundingBox], [x[0] + 1 for x in boundingBox], shape=shape) rr5, cc5 = draw.polygon_perimeter( [x[1] for x in boundingBox], [x[0] - 1 for x in boundingBox], shape=shape) boxColor = (int(255 * (1 - (confidence**2))), int(255 * (confidence**2)), 0) draw.set_color(image, (rr, cc), boxColor, alpha=0.8) draw.set_color(image, (rr2, cc2), boxColor, alpha=0.8) draw.set_color(image, (rr3, cc3), boxColor, alpha=0.8) draw.set_color(image, (rr4, cc4), boxColor, alpha=0.8) draw.set_color(image, (rr5, cc5), boxColor, alpha=0.8) if not make_image_only: io.imshow(image) io.show() detections = { 'detections': detections, 'image': image, 'caption': '\n<br/>'.join(imcaption) } except Exception as e: print('Unable to show image: ' + str(e)) results = [] sub_detections = detections[ 'detections'] if 'detections' in detections else detections for detection in sub_detections: class_name, class_confidence, bbox = detection x, y, w, h = bbox x_min = int(x - (w / 2)) y_min = int(y - (h / 2)) results.append( DarkNetPredictionResult(class_name=class_name, class_confidence=class_confidence, left_x=max(0, int(x_min)), top_y=max(0, int(y_min)), width=max(0, int(w)), height=max(0, int(h)))) self.lock.release() return results
def main(): np.set_printoptions(threshold=np.inf) context_cloud = zmq.Context() socket_cloud = context_cloud.socket(zmq.SUB) socket_cloud.setsockopt(zmq.SUBSCRIBE, b"") socket_cloud.setsockopt(zmq.RCVHWM, 1) socket_cloud.connect("tcp://localhost:5556") print("Collecting point clouds...") context_result = zmq.Context() socket_result = context_result.socket(zmq.SUB) socket_result.setsockopt(zmq.SUBSCRIBE, b"") socket_result.setsockopt(zmq.RCVHWM, 1) socket_result.connect("tcp://localhost:5560") print("Collecting inference...") context_odom_list = [] socket_odom_car_list = [] for ind in range(len(car_ips)): context_odom_list.append(zmq.Context()) socket_odom_car_list.append(context_odom_list[ind].socket(zmq.SUB)) for ind in range(len(socket_odom_car_list)): socket_odom_car = socket_odom_car_list[ind] socket_odom_car.setsockopt(zmq.SUBSCRIBE, b"") socket_odom_car.setsockopt(zmq.RCVHWM, 1) print("Collecting odom info from car {}...".format(ind)) socket_odom_car.connect(car_ips[ind]) context_box = zmq.Context() socket_box = context_box.socket(zmq.PUB) socket_box.setsockopt(zmq.SNDHWM, 1) socket_box.bind("tcp://*:5557") print('Sending bbox') context_grid = zmq.Context() socket_grid = context_grid.socket(zmq.PUB) socket_grid.setsockopt(zmq.SNDHWM, 1) socket_grid.bind("tcp://*:5558") print('Sending occupancy grid') # create occupancy grid dim_x = int((x_clip[1] - x_clip[0]) * grid_res) dim_y = int((y_clip[1] - y_clip[0]) * grid_res) dim_x_object = int((x_clip[1] - x_clip[0]) * object_res) dim_y_object = int((y_clip[1] - y_clip[0]) * object_res) object_matrix = np.zeros([dim_x_object, dim_y_object]) object_matrix_copy = object_matrix.copy() car_matrix = np.zeros([dim_x_object, dim_y_object]) car_matrix_object = np.zeros([dim_x_object, dim_y_object]) car_peri_coords_x = [] car_peri_coords_y = [] pc_grid = np.zeros((1, 2)) pc_in = None inference_in = None odom_info = None match_inds = np.zeros((control_row.shape[0], )) first_time = True bbox_arrays = np.zeros((matching_row.shape[0], 10, 3)) while True: occupancy_grid = OccupancyGrid(dim_x, dim_y) # get new point cloud if socket_cloud.poll(timeout=5) != 0: # add objects in the grid pc_in = np.transpose(recv_array(socket_cloud)) pc = pc_in[np.where((pc_in[:, 1] > y_clip[0]) & (pc_in[:, 1] < y_clip[1]))] pc = pc[np.where(pc[:, 0] < x_clip[1])] pc[:, 2] += -z_clip pc = pc[np.where((pc[:, 2] > 0))] pc_grid = pc[:, 0:2] # from occupancy grid pc_grid[:, 0] = (np.floor(pc_grid[:, 0] * object_res) + dim_x_object / 2) pc_grid[:, 1] = (np.floor(pc_grid[:, 1] * object_res) + dim_y_object / 2) pc_grid = pc_grid.astype(int) if pc_grid.shape[0] > 1: object_matrix = np.zeros([dim_x_object, dim_y_object]) pc_grid, counts = np.unique(pc_grid, return_counts=True, axis=0) pc_grid = pc_grid[np.where(counts > grid_res / object_res)] object_matrix[pc_grid[:, 0], pc_grid[:, 1]] = 1 object_matrix_copy = object_matrix.copy() # get new bounding box if socket_result.poll(timeout=1) != 0: inference_in = recv_array(socket_result).copy() first_time = False # clean the matching row for row_ind in range(matching_row.shape[0]): if matching_row[row_ind, 0] > 0: if inference_in[1, 1, 0] - matching_row[ row_ind, 7] > 1: # discard the object if we lose track of it for 1 seconds # print('discarded', matching_row[row_ind, :]) matching_row[row_ind, :] = np.zeros((12)) num_dt = inference_in[0, 0, 0] # looking for matches in the matching_row for dt_ind in range(int(num_dt)): if dt_ind >= matching_row.shape[0]: break distances = np.sqrt( (matching_row[:, 1] - inference_in[dt_ind + 1, 0, 0])**2 + (matching_row[:, 2] - inference_in[dt_ind + 1, 0, 1])**2) matches = np.argsort(distances) # print(distances) # print(matches[0]) if distances[matches[ 0]] >= matching_distance_threshold: # Is the detection in match_row? for row_ind in range( matching_row.shape[0] ): # Find an empty row to put this detection if matching_row[row_ind, 0] == 0: matching_row[ row_ind, 0] = 9 # indicator, 9 means it's unknown matching_row[row_ind, 1] = inference_in[dt_ind + 1, 0, 0] # x matching_row[row_ind, 2] = inference_in[dt_ind + 1, 0, 1] # y matching_row[row_ind, 3] = inference_in[dt_ind + 1, 0, 2] # z matching_row[row_ind, 4] = 0.28 # width matching_row[row_ind, 5] = 0.57 # length matching_row[row_ind, 6] = inference_in[dt_ind + 1, 0, 5] # height matching_row[row_ind, 7] = inference_in[dt_ind + 1, 1, 0] # time matching_row[row_ind, 8] = inference_in[dt_ind + 1, 0, 6] # theta matching_row[row_ind, 9] = inference_in[dt_ind + 1, 1, 1] # score # matching_row[row_ind, 10] detection_count # matching_row[row_ind, 11] flip_count break else: row_ind = matches[0] if not (matching_row[row_ind, 10] == calibration_count and matching_row[row_ind, 11] / matching_row[row_ind, 10] >= 0.5): inference_in[ dt_ind + 1, 0, 6] -= np.pi # only make adjustment at the end of calibration matching_row[row_ind, 1] = inference_in[dt_ind + 1, 0, 0] # x matching_row[row_ind, 2] = inference_in[dt_ind + 1, 0, 1] # y matching_row[row_ind, 3] = inference_in[dt_ind + 1, 0, 2] # z matching_row[row_ind, 4] = 0.28 # width matching_row[row_ind, 5] = 0.57 # length matching_row[row_ind, 6] = inference_in[dt_ind + 1, 0, 5] # height matching_row[row_ind, 8] = inference_in[dt_ind + 1, 0, 6] # theta matching_row[row_ind, 9] = inference_in[dt_ind + 1, 1, 1] # score delta_time = inference_in[dt_ind + 1, 1, 0] - matching_row[row_ind, 7] if np.abs(np.pi - np.abs(matching_row[row_ind, 8] - inference_in[dt_ind + 1, 0, 6]) ) < 0.6 and delta_time != 0: if matching_row[ row_ind, 10] < calibration_count and matching_row[ row_ind, 9] > 0.6: matching_row[row_ind, 11] += 1 # flip_count matching_row[row_ind, 7] = inference_in[dt_ind + 1, 1, 0] # time # print('matching_row', matching_row) for control_row_ind in range(control_row.shape[0]): distances = np.sqrt( (matching_row[:, 1] - control_row[control_row_ind, 0])**2 + (matching_row[:, 2] - control_row[control_row_ind, 1])**2) matches = np.argsort(distances) if distances[matches[0]] <= matching_distance_threshold: match_inds[control_row_ind] = matches[0] control_row[control_row_ind, 0:3] = control_row[ control_row_ind, 0:3] + K * (matching_row[matches[0], 1:4] - control_row[control_row_ind, 0:3]) control_row[control_row_ind, 3] = control_row[control_row_ind, 3] + K * ( matching_row[matches[0], 8] - control_row[control_row_ind, 3]) matching_row[ matches[0], 0] = control_row_ind + 1 # index for visualization # print(control_row_ind) # print('matching_row', matching_row[int(match_inds[control_row_ind]), 1:5]) # print('control_row', control_row[control_row_ind, 0:5]) # create and send bbox info for row_ind in range(matching_row.shape[0]): bbox_array = np.zeros((10, 3)) if matching_row[row_ind, 0] == 0: bbox_arrays[row_ind] = bbox_array continue if matching_row[row_ind, 10] < calibration_count and matching_row[ row_ind, 9] > 0.6: matching_row[row_ind, 10] += 1 elif matching_row[row_ind, 10] == calibration_count: print('Calibrated') matching_row[row_ind, 10] += 1 print('ID:', matching_row[row_ind, 0]) print('Position:', matching_row[row_ind, 1:4], matching_row[row_ind, 8]) bbox_arrays[row_ind] = prepare_bbox_array( matching_row, row_ind).copy() # print(bbox_arrays) # odometry update control_row_ind = 0 socket_odom_car = socket_odom_car_list[ control_row_ind] # only have one car for now if socket_odom_car.poll(timeout=1) != 0: odom_info = recv_array(socket_odom_car).copy() # control_row[control_row_ind] is the mu # matching_row[match_inds[control_row_ind]] is the z matching_row_ind = int(match_inds[control_row_ind]) linear_speed_correction = 0.8 angular_speed_correction = 0.7 delta_time_odom = odom_info[2] - control_row[control_row_ind, 4] control_row[control_row_ind, 4] = odom_info[2] # predict step if matching_row[ matching_row_ind, 7] != 0 and delta_time_odom > 0.01 and delta_time_odom < 0.1: car_movement = convert_odom( odom_info[0] * linear_speed_correction, odom_info[1] * angular_speed_correction, control_row[control_row_ind, 3]) # print('delta_time_odom * car_movement', delta_time_odom * car_movement) # print('control_row[control_row_ind, 0:4]', control_row[control_row_ind, 0:4]) control_row[ control_row_ind, 0:4] = delta_time_odom * car_movement + control_row[ control_row_ind, 0:4] matching_row[matching_row_ind, 1:4] = control_row[control_row_ind, 0:3] matching_row[matching_row_ind, 8] = control_row[control_row_ind, 3] # create and send bbox info # for control_row_ind in range(len(match_inds)): matching_row_ind = int(match_inds[control_row_ind]) bbox_arrays[matching_row_ind] = prepare_bbox_array( matching_row, matching_row_ind).copy() send_array(socket_box, bbox_arrays) # occupancy grid update if first_time == False and pc_grid.shape[0] > 1: # if False: car_matrix = np.zeros([dim_x_object, dim_y_object]) # print(bbox_arrays.shape) for box_ind in range(bbox_arrays.shape[0]): try: # add car detection in the grid rect_x = np.zeros((4, )) rect_y = np.zeros((4, )) rect_x[0] = find_coords(bbox_arrays[box_ind, 0, 0], object_res, dim_x_object / 2) rect_y[0] = find_coords(bbox_arrays[box_ind, 0, 1], object_res, dim_y_object / 2) rect_x[1] = find_coords(bbox_arrays[box_ind, 4, 0], object_res, dim_x_object / 2) rect_y[1] = find_coords(bbox_arrays[box_ind, 4, 1], object_res, dim_y_object / 2) rect_x[2] = find_coords(bbox_arrays[box_ind, 6, 0], object_res, dim_x_object / 2) rect_y[2] = find_coords(bbox_arrays[box_ind, 6, 1], object_res, dim_y_object / 2) rect_x[3] = find_coords(bbox_arrays[box_ind, 2, 0], object_res, dim_x_object / 2) rect_y[3] = find_coords(bbox_arrays[box_ind, 2, 1], object_res, dim_y_object / 2) car_coords_x, car_coords_y = np.array( polygon(rect_x, rect_y, shape=(dim_x_object, dim_y_object))) car_matrix[car_coords_x, car_coords_y] = 1 rect_x[0] = find_coords(bbox_arrays[box_ind, 0, 0], grid_res, dim_x / 2) rect_y[0] = find_coords(bbox_arrays[box_ind, 0, 1], grid_res, dim_y / 2) rect_x[1] = find_coords(bbox_arrays[box_ind, 4, 0], grid_res, dim_x / 2) rect_y[1] = find_coords(bbox_arrays[box_ind, 4, 1], grid_res, dim_y / 2) rect_x[2] = find_coords(bbox_arrays[box_ind, 6, 0], grid_res, dim_x / 2) rect_y[2] = find_coords(bbox_arrays[box_ind, 6, 1], grid_res, dim_y / 2) rect_x[3] = find_coords(bbox_arrays[box_ind, 2, 0], grid_res, dim_x / 2) rect_y[3] = find_coords(bbox_arrays[box_ind, 2, 1], grid_res, dim_y / 2) car_peri_coords_x, car_peri_coords_y = np.array( polygon_perimeter(rect_x, rect_y, shape=(dim_x, dim_y), clip=True)) # if an occupied grid's neighbor is in car, then that grid is also in car car_matrix_object = np.zeros([dim_x_object, dim_y_object]) pc_grid = np.transpose( np.array(np.where(object_matrix == 1))) for ind in range(pc_grid.shape[0]): if car_matrix[pc_grid[ind, 0], pc_grid[ind, 1]] == 1: car_matrix_object[pc_grid[ind, 0], pc_grid[ind, 1]] = 1 else: find_neighbor = 0 neighbors = find_neighbours(pc_grid[ind, 0], pc_grid[ind, 1], dim_x_object, dim_y_object, option=0) for neighbor in neighbors: if object_matrix[neighbor[0], neighbor[1]] == 1: find_neighbor = 1 if car_matrix[neighbor[0], neighbor[1]] == 1: car_matrix_object[pc_grid[ind, 0], pc_grid[ind, 1]] = 1 find_neighbor = 1 continue if find_neighbor == 0: # remove isolated points object_matrix_copy[pc_grid[ind, 0], pc_grid[ind, 1]] = 0 # use connected body to find the car matrix_labels, num = label(object_matrix_copy, connectivity=2, return_num=True) find_flag = 0 for num_ind in range(num + 1): label_xy = np.array(np.where(matrix_labels == num_ind)) if num_ind != 0: for ind in range(label_xy.shape[1]): if car_matrix_object[label_xy[0, ind], label_xy[1, ind]] == 1: car_matrix_object[label_xy[0, :], label_xy[1, :]] = 1 find_flag = 1 if find_flag == 1: break # print(label_xy.shape[1], ind) pc_grid = np.transpose( np.array(np.where(car_matrix_object == 1))) object_matrix_copy[pc_grid[:, 0], pc_grid[:, 1]] = 0 car_matrix_object_big = rescale(car_matrix_object, grid_res / object_res, anti_aliasing=False) occupancy_grid.matrix[np.where( car_matrix_object_big > 0)] = 2 occupancy_grid.matrix[ car_peri_coords_x, car_peri_coords_y] = 10 + bbox_arrays[ box_ind, 9, 1] # calibration number except (IndexError): continue object_matrix_big = rescale(object_matrix_copy, grid_res / object_res, anti_aliasing=False) occupancy_grid.matrix[np.where(object_matrix_big > 0)] = 1 occupancy_grid.update_image() car_matrix_big = rescale(car_matrix, grid_res / object_res, anti_aliasing=False) car_matrix_big[np.where(car_matrix_big > 0)] = 1 # np.savez('car_info', occupancy_grid.matrix, occupancy_grid.image, np.array([find_coords(bbox_array[:, 8, 0], grid_res), find_coords(bbox_array[:, 8, 1], grid_res, dim_y/2)]), np.array(bbox_array[:, 9, 0]), car_matrix_big) send_array(socket_grid, occupancy_grid.image) else: object_matrix_big = rescale(object_matrix_copy, grid_res / object_res, anti_aliasing=False) occupancy_grid.matrix[np.where(object_matrix_big > 0)] = 1 occupancy_grid.update_image() # print(object_matrix_copy.shape) send_array(socket_grid, occupancy_grid.image)
def test_polygon_perimeter_outside_image(): rr, cc = polygon_perimeter([-1, -1, 3, 3], [-1, 4, 4, -1], shape=(3, 4)) assert_equal(len(rr), 0) assert_equal(len(cc), 0)
def performPredict(imagePath="data/dog.jpg", thresh=0.25, outPath=None): """ Convenience function to handle the detection and returns of objects. Displaying bounding boxes requires libraries scikit-image and numpy Parameters ---------------- imagePath: str Path to the image to evaluate. Raises ValueError if not found thresh: float (default= 0.25) The detection threshold Returns ---------------------- """ print("Perform prediction\n") # Import the global variables. This lets us instance Darknet once, then just call performDetect() again without instancing again global metaMain, netMain, altNames # pylint: disable=W0603 assert 0 < thresh < 1, "Threshold should be a float between zero and one (non-inclusive)" if not os.path.exists(imagePath): raise ValueError("Invalid image path `" + os.path.abspath(imagePath) + "`") # Do the detection detections = detect(netMain, metaMain, imagePath.encode("utf-8"), thresh) saveImage = False if outPath is not None: saveImage = True outDir = Path(outPath).parent if os.path.exists(outDir): shutil.rmtree(outDir) # delete output folder os.makedirs(outDir) # make new output folder if saveImage: try: from skimage import io, draw import numpy as np image = io.imread(imagePath) print("*** "+str(len(detections))+" Results, color coded by confidence ***") imcaption = [] for detection in detections: label = detection[0] confidence = detection[1] pstring = str(label, encoding="utf-8")+": "+str(np.rint(100 * confidence))+"%" imcaption.append(pstring) #print(pstring) bounds = detection[2] shape = image.shape yExtent = int(bounds[3]) xEntent = int(bounds[2]) # Coordinates are around the center xCoord = int(bounds[0] - bounds[2]/2) yCoord = int(bounds[1] - bounds[3]/2) boundingBox = [ [xCoord, yCoord], [xCoord, yCoord + yExtent], [xCoord + xEntent, yCoord + yExtent], [xCoord + xEntent, yCoord] ] # Wiggle it around to make a 3px border rr, cc = draw.polygon_perimeter([x[1] for x in boundingBox], [x[0] for x in boundingBox], shape= shape) rr2, cc2 = draw.polygon_perimeter([x[1] + 1 for x in boundingBox], [x[0] for x in boundingBox], shape= shape) rr3, cc3 = draw.polygon_perimeter([x[1] - 1 for x in boundingBox], [x[0] for x in boundingBox], shape= shape) rr4, cc4 = draw.polygon_perimeter([x[1] for x in boundingBox], [x[0] + 1 for x in boundingBox], shape= shape) rr5, cc5 = draw.polygon_perimeter([x[1] for x in boundingBox], [x[0] - 1 for x in boundingBox], shape= shape) boxColor = (int(255 * (1 - (confidence ** 2))), int(255 * (confidence ** 2)), 0) draw.set_color(image, (rr, cc), boxColor, alpha= 0.8) draw.set_color(image, (rr2, cc2), boxColor, alpha= 0.8) draw.set_color(image, (rr3, cc3), boxColor, alpha= 0.8) draw.set_color(image, (rr4, cc4), boxColor, alpha= 0.8) draw.set_color(image, (rr5, cc5), boxColor, alpha= 0.8) #if not makeImageOnly: save_path = str(Path(outPath)) txt_path = str(Path(outDir) / Path(outPath).stem) results = detections2json(detections) io.imsave(save_path, image) with open(txt_path + '.json', 'a') as f: f.write(json.dumps(results, indent=2, ensure_ascii=False)) #io.imshow(image) #io.show() except Exception as e: print("Unable to show image: "+str(e)) return detections
#5 in rand uniform to get distinguishable shapes (min radius) radius = np.random.uniform(5,grid_size/2,perType) #Random radius for each sample thetas = np.linspace(0,2*np.pi,shapeSides[i]+1) #Linear spacing of theta (Regular Poly) for j in range(perType): thetas += np.random.uniform(0,2*np.pi) #Random phase (rotation) #Calculate vertices points = np.empty((2,shapeSides[i]+1)) points[0,:] = (radius[j]*np.cos(thetas) + grid_size/2) points[0,:] = [int(pt) for pt in points[0,:]] #Convert to int points[1,:] = (radius[j]*np.sin(thetas) + grid_size/2) points[1,:] = [int(pt) for pt in points[1,:]] #Convert to int rows, columns = polygon_perimeter(points[0,:], points[1,:],\ shape = [grid_size, grid_size], clip = True) dataIMGs[i*perType + j,rows,columns] = 1 #Image curves = np.zeros((shapeSides[i],2,2)) for k in range(shapeSides[i]): curves[k] = points[:,k:(k+2)] dataCurves.append(curves) #Segmentation of the curves for i in range(len(dataCurves)): dataCurves[i] = segmentation(dataCurves[i]) np.save(imgs, dataIMGs) #Save Generated Data Images np.save(crvs, dataCurves) #Save Generated Bezier Curves
def performDetect(calibrate=True, f=0.00415, imagePath="data/dog.jpg", thresh=0.25, configPath="./cfg/yolov4.cfg", weightPath="yolov4.weights", metaPath="./cfg/coco.data", showImage=True, makeImageOnly=False, initOnly=False, SD=0): """ Convenience function to handle the detection and returns of objects. Displaying bounding boxes requires libraries scikit-image and numpy Parameters ---------------- imagePath: str Path to the image to evaluate. Raises ValueError if not found thresh: float (default= 0.25) The detection threshold configPath: str Path to the configuration file. Raises ValueError if not found weightPath: str Path to the weights file. Raises ValueError if not found metaPath: str Path to the data file. Raises ValueError if not found showImage: bool (default= True) Compute (and show) bounding boxes. Changes return. makeImageOnly: bool (default= False) If showImage is True, this won't actually *show* the image, but will create the array and return it. initOnly: bool (default= False) Only initialize globals. Don't actually run a prediction. Returns ---------------------- When showImage is False, list of tuples like ('obj_label', confidence, (bounding_box_x_px, bounding_box_y_px, bounding_box_width_px, bounding_box_height_px)) The X and Y coordinates are from the center of the bounding box. Subtract half the width or height to get the lower corner. Otherwise, a dict with { "detections": as above "image": a numpy array representing an image, compatible with scikit-image "caption": an image caption } """ # Import the global variables. This lets us instance Darknet once, then just call performDetect() again without instancing again global metaMain, netMain, altNames #pylint: disable=W0603 assert 0 < thresh < 1, "Threshold should be a float between zero and one (non-inclusive)" if not os.path.exists(configPath): raise ValueError("Invalid config path `" + os.path.abspath(configPath) + "`") if not os.path.exists(weightPath): raise ValueError("Invalid weight path `" + os.path.abspath(weightPath) + "`") if not os.path.exists(metaPath): raise ValueError("Invalid data file path `" + os.path.abspath(metaPath) + "`") if netMain is None: netMain = load_net_custom(configPath.encode("ascii"), weightPath.encode("ascii"), 0, 1) # batch size = 1 if metaMain is None: metaMain = load_meta(metaPath.encode("ascii")) if altNames is None: # In Python 3, the metafile default access craps out on Windows (but not Linux) # Read the names file and create a list to feed to detect try: with open(metaPath) as metaFH: metaContents = metaFH.read() import re match = re.search("names *= *(.*)$", metaContents, re.IGNORECASE | re.MULTILINE) if match: result = match.group(1) else: result = None try: if os.path.exists(result): with open(result) as namesFH: namesList = namesFH.read().strip().split("\n") altNames = [x.strip() for x in namesList] except TypeError: pass except Exception: pass if initOnly: print("Initialized detector") return None if not os.path.exists(imagePath): raise ValueError("Invalid image path `" + os.path.abspath(imagePath) + "`") # Do the detection #detections = detect(netMain, metaMain, imagePath, thresh) # if is used cv2.imread(image) detections = detect(netMain, metaMain, imagePath.encode("ascii"), thresh) if showImage: #try: image = io.imread(imagePath) #image = cv2.resize(image, (darknet.network_width(netMain), darknet.network_height(netMain)), interpolation=cv2.INTER_LINEAR) print("*** " + str(len(detections)) + " Results, color coded by confidence ***") imcaption = [] face_mids = [] person_feet = [] xywh = [] wp = [] hp = [] i = 0 #SD = 0 sensor_w, sensor_h = 4.8, 3.6 sensor_w_px, sensor_h_px = 3200, 2400 f = f * 1000 * darknet.network_width(netMain) / sensor_w for detection in detections: label = detection[0] confidence = detection[1] pstring = label + ": " + str(np.rint(100 * confidence)) + "%" imcaption.append(pstring) print(pstring) bounds = detection[2] shape = image.shape # x = shape[1] # xExtent = int(x * bounds[2] / 100) # y = shape[0] # yExtent = int(y * bounds[3] / 100) yExtent = int(bounds[3]) xExtent = int(bounds[2]) # Coordinates are around the center xCoord = int(bounds[0] - bounds[2] / 2) yCoord = int(bounds[1] - bounds[3] / 2) x, y, w, h = xCoord, yCoord, int(bounds[2]), int(bounds[3]) coord = [x, y, w, h] x_mid, y_mid = bounds[0], bounds[1] mid_coord = (x_mid, y_mid) face_mids.append(mid_coord) if (label == 'Person'): print(i) xywh.append(coord) wp.append(w) hp.append(h) i += 1 boundingBox = [[xCoord, yCoord], [xCoord, yCoord + yExtent], [xCoord + xExtent, yCoord + yExtent], [xCoord + xExtent, yCoord]] font_scale = 0.35 thickness = 1 blue = (0, 0, 255) green = (0, 255, 0) red = (255, 0, 0) font = cv2.FONT_HERSHEY_COMPLEX # Wiggle it around to make a 3px border rr, cc = draw.polygon_perimeter([x[1] for x in boundingBox], [x[0] for x in boundingBox], shape=shape) rr2, cc2 = draw.polygon_perimeter([x[1] + 1 for x in boundingBox], [x[0] for x in boundingBox], shape=shape) rr3, cc3 = draw.polygon_perimeter([x[1] - 1 for x in boundingBox], [x[0] for x in boundingBox], shape=shape) rr4, cc4 = draw.polygon_perimeter([x[1] for x in boundingBox], [x[0] + 1 for x in boundingBox], shape=shape) rr5, cc5 = draw.polygon_perimeter([x[1] for x in boundingBox], [x[0] - 1 for x in boundingBox], shape=shape) if (label == 'Mask'): boxColor = green cv2.putText(image, "Mask", (x, y - 10), font, font_scale, green, thickness) cv2.rectangle(image, (x, y), (x + w, y + h), green, 2) elif (label == 'No_mask'): boxColor = red cv2.putText(image, "No Mask", (x, y - 10), font, font_scale, red, thickness) cv2.rectangle(image, (x, y), (x + w, y + h), red, 2) elif (label == 'Person'): x_pmid = x + w / 2 y_pmid = y + h feet_coord = (x_pmid, y_pmid) person_feet.append(feet_coord) #boxColor = (int(255 * (1 - (confidence ** 2))), int(255 * (confidence ** 2)), 0) #correct_peeps = [] #wrong_peeps = [] if (calibrate == True): x1, y1 = person_feet[0] x2, y2 = person_feet[1] w1 = wp[0] w2 = wp[1] h1 = hp[0] h2 = hp[1] v1 = 1.6 * f / (h1) v2 = 1.6 * f / (h2) real_dist = sensor_w * abs(x1 - x2) / sensor_w_px x1_ = 0 x2_ = real_dist SD = math.sqrt((x1_ - x2_) * (x1_ - x2_) + (v1 - v2) * (v1 - v2)) print("Calibrated at ", SD) print(v1, v2) x1 = int(x1) y1 = int(y1) w1 = int(w1) h1 = int(h1) x2 = int(x2) y2 = int(y2) w2 = int(w2) h2 = int(h2) #cv2.rectangle(image, (x1, y1), (x1 + w1, y1 + h1), (150, 150, 0), 2) #cv2.rectangle(image, (x2, y2), (x2 + w2, y2 + h2), (150, 150, 0), 2) cv2.circle(image, (x1, y1), 25, (0, 0, 255), -1) cv2.circle(image, (x2, y2), 25, (0, 0, 255), -1) io.imshow(image) cv2.imwrite('result.jpg', image) cv2_imshow(image) ref_cords = [(x1, y1), (x2, y2)] io.show() return SD sd_main = [] i = 0 j = 0 print("Calibrated at ", SD) for mid1 in person_feet: truth = True j = 0 for mid2 in person_feet: sd = check(SD, mid1, mid2, wp[i], wp[j], hp[i], hp[j], f) print(i, " -> ", j, " = ", sd) if (sd == False): truth = False break j += 1 i += 1 sd_main.append(truth) i = 0 for coord in xywh: x, y, w, h = coord if (sd_main[i] == True): print("SD") boxColor = (150, 150, 0) cv2.rectangle(image, (int(x), int(y)), (x + w, y + h), (150, 150, 0), 3) cv2.putText(image, str(i) + " SD", (x, y - 10), font, font_scale, (150, 150, 0), thickness) else: print("NO SD") boxColor = (0, 150, 150) cv2.rectangle(image, (int(x), int(y)), (x + w, y + h), (150, 0, 0), 3) cv2.putText(image, str(i) + " No SD", (x, y - 10), font, font_scale, (0, 0, 150), thickness) i += 1 ''' draw.set_color(image, (rr, cc), boxColor, alpha= 0.8) draw.set_color(image, (rr2, cc2), boxColor, alpha= 0.8) draw.set_color(image, (rr3, cc3), boxColor, alpha= 0.8) draw.set_color(image, (rr4, cc4), boxColor, alpha= 0.8) draw.set_color(image, (rr5, cc5), boxColor, alpha= 0.8)''' if not makeImageOnly: io.imshow(image) cv2.imwrite('result.jpg', image) #cv2_imshow(image) io.show() detections = { "detections": detections, "image": image, "caption": "\n<br/>".join(imcaption) } #except Exception as e: #print("Unable to show image: "+str(e)) #return detections return detections
def test_img_plot( img_name, img_read_loc, img_read_format='.tif', img_save_loc=None, img_save_format='png', tly=123, bly=415, bry=427, trry=136, part_cx=False, part_cy=False, draw_bpe_color=None, draw_particle_color=None, draw_particle=True, draw_bpe=True, show=True, save=False, pause_time=3, um_per_pix=1, bf=35, draw_particle_radius=10, ): # other variables lr_shift_y = 14 # angle of image w = 550 # channel width # read file path img_name = img_name + '_X1' image_read = img_read_loc + img_name + img_read_format # load image img = io.imread(image_read) # calculate image shape img_dims = np.shape(img) # calculate BPE locations centerline = int( np.round((np.mean([tly, bly]) + np.mean([bry, trry])) / 2, 0)) bpe_leadedge_centerline = int(np.round((np.mean([tly, bly])))) # draw bpe rectangle if draw_bpe == True: # choose color if draw_bpe_color == None: draw_bpe_color = int(np.round(bf * np.mean(img), 0)) # organize particle data xloc = int(np.round((part_cx) / um_per_pix, 0)) yloc = int(np.round((part_cy) / um_per_pix, 0)) # draw rectangle poly = np.array( ((yloc - thresh, xloc - thresh), (yloc - thresh, xloc + thresh), (yloc + thresh, xloc + thresh), (yloc + thresh, xloc - thresh), (yloc - thresh, xloc - thresh))) rr, cc = polygon_perimeter(poly[:, 0], poly[:, 1], img.shape) img[rr, cc] = draw_bpe_color # rescale back to micron coordinates and adjust image name xloc = int(np.round(xloc * um_per_pix, 0)) yloc = int(np.round(yloc * um_per_pix, 0)) # draw centerline line #rr, cc = line(centerline, 0, centerline, 511) #img[rr, cc] = draw_bpe_color # draw wall lines rr, cc = line( int(np.round(centerline + (w / um_per_pix) / 2, 0) - 6), 0, int( np.round(centerline + (w / um_per_pix) / 2 + lr_shift_y, 0) - 6), 511) img[rr, cc] = draw_bpe_color rr, cc = line( int(np.round(centerline - (w / um_per_pix) / 2, 0) - 6), 0, int( np.round(centerline - (w / um_per_pix) / 2 + lr_shift_y, 0) - 6), 511) img[rr, cc] = draw_bpe_color # draw particle if draw_particle == True: # choose color if draw_particle_color == None: draw_particle_color = int(np.round(np.min(img), 0)) # organize particle data xloc = int(np.round((part_cx) / um_per_pix, 0)) yloc = int(np.round((part_cy) / um_per_pix, 0)) # create circle perimeter rr, cc = circle_perimeter(yloc, xloc, draw_particle_radius) rr = np.where(rr > 511, 511, rr) rr = np.where(rr < 0, 0, rr) cc = np.where(cc > 511, 511, cc) cc = np.where(cc < 0, 0, cc) img[rr, cc] = draw_particle_color # rescale back to micron coordinates xloc = int(np.round(xloc * um_per_pix, 0)) yloc = int(np.round(yloc * um_per_pix, 0)) # adjust image name img_name = "Image-Particle_X" + str(xloc) + '.Y' + str(yloc) # create figure fig, ax = plt.subplots(figsize=(8, 8)) ax.imshow(img * bf, cmap='gray', extent=(0, img_dims[0] * um_per_pix, img_dims[1] * um_per_pix, 0)) ax.set_title(img_name) plt.xlabel('Axial - Channel (um)') plt.ylabel('Transverse - Channel (um)') plt.grid(color='dimgray', alpha=0.25, linestyle='-', linewidth=0.25) # display if show == True: plt.show() plt.pause(pause_time) # save if save == True: image_save = img_save_loc + img_name + '.' + img_save_format #plt.imsave(image_save, img * bf, format=img_save_format, cmap='gray') #plt_img_save = img_save_loc + img_name + '_plt.' + img_save_format print(image_save) plt.savefig(image_save, format=img_save_format) plt.close(fig=None) # return centerline return centerline
def add_mask(self, points=None, color=(100, 200, 50)): # loop over z values median = [] if points.shape[1] < 3: points = np.concatenate((np.zeros( (points.shape[0], 1), np.int32), points), axis=1) zdraw = np.unique(points[:, 0]) zrange = np.arange(zdraw.min(), zdraw.max() + 1, 1, int) zmin = zdraw.min() pix = np.zeros((2, 0), np.uint16) mall = np.zeros((len(zrange), self.Ly, self.Lx), np.bool) k = 0 for z in zdraw: iz = points[:, 0] == z vr = points[iz, 1] vc = points[iz, 2] vr, vc = draw.polygon_perimeter(vr, vc, self.layers[z].shape[:2]) ar, ac = draw.polygon(vr, vc, self.layers[z].shape[:2]) ar, ac = np.hstack((np.vstack((vr, vc)), np.vstack((ar, ac)))) # if these pixels are overlapping with another cell, reassign them ioverlap = self.cellpix[z][ar, ac] > 0 if (~ioverlap).sum() < 8: print('ERROR: cell too small without overlaps, not drawn') return None elif ioverlap.sum() > 0: ar, ac = ar[~ioverlap], ac[~ioverlap] # compute outline of new mask mask = np.zeros((np.ptp(ar) + 4, np.ptp(ac) + 4), np.uint8) mask[ar - ar.min() + 2, ac - ac.min() + 2] = 1 outlines = plot.masks_to_outlines(mask) vr, vc = np.nonzero(outlines) vr, vc = vr + ar.min() - 2, vc + ac.min() - 2 self.draw_mask(z, ar, ac, vr, vc, color) median.append(np.array([np.median(ar), np.median(ac)])) mall[z - zmin, ar, ac] = True pix = np.append(pix, np.vstack((ar, ac)), axis=-1) mall = mall[:, pix[0].min():pix[0].max() + 1, pix[1].min():pix[1].max() + 1].astype(np.float32) ymin, xmin = pix[0].min(), pix[1].min() if len(zdraw) > 1: mall, zfill = interpZ(mall, zdraw - zmin) for z in zfill: mask = mall[z].copy() ar, ac = np.nonzero(mask) ioverlap = self.cellpix[z + zmin][ar + ymin, ac + xmin] > 0 if (~ioverlap).sum() < 5: print( 'WARNING: stroke on plane %d not included due to overlaps' % z) elif ioverlap.sum() > 0: mask[ar[ioverlap], ac[ioverlap]] = 0 ar, ac = ar[~ioverlap], ac[~ioverlap] # compute outline of mask outlines = plot.masks_to_outlines(mask) vr, vc = np.nonzero(outlines) vr, vc = vr + ymin, vc + xmin ar, ac = ar + ymin, ac + xmin self.draw_mask(z + zmin, ar, ac, vr, vc, color) self.zdraw.append(zdraw) return median
def performDetect(imagePath="data/dog.jpg", thresh= 0.25, configPath = "./cfg/yolov3.cfg", weightPath = "yolov3.weights", metaPath= "./cfg/coco.data", showImage= True, makeImageOnly = False, initOnly= False): """ Convenience function to handle the detection and returns of objects. Displaying bounding boxes requires libraries scikit-image and numpy Parameters ---------------- imagePath: str Path to the image to evaluate. Raises ValueError if not found thresh: float (default= 0.25) The detection threshold configPath: str Path to the configuration file. Raises ValueError if not found weightPath: str Path to the weights file. Raises ValueError if not found metaPath: str Path to the data file. Raises ValueError if not found showImage: bool (default= True) Compute (and show) bounding boxes. Changes return. makeImageOnly: bool (default= False) If showImage is True, this won't actually *show* the image, but will create the array and return it. initOnly: bool (default= False) Only initialize globals. Don't actually run a prediction. Returns ---------------------- When showImage is False, list of tuples like ('obj_label', confidence, (bounding_box_x_px, bounding_box_y_px, bounding_box_width_px, bounding_box_height_px)) The X and Y coordinates are from the center of the bounding box. Subtract half the width or height to get the lower corner. Otherwise, a dict with { "detections": as above "image": a numpy array representing an image, compatible with scikit-image "caption": an image caption } """ # Import the global variables. This lets us instance Darknet once, then just call performDetect() again without instancing again global metaMain, netMain, altNames #pylint: disable=W0603 assert 0 < thresh < 1, "Threshold should be a float between zero and one (non-inclusive)" if not os.path.exists(configPath): raise ValueError("Invalid config path `"+os.path.abspath(configPath)+"`") if not os.path.exists(weightPath): raise ValueError("Invalid weight path `"+os.path.abspath(weightPath)+"`") if not os.path.exists(metaPath): raise ValueError("Invalid data file path `"+os.path.abspath(metaPath)+"`") if netMain is None: netMain = load_net_custom(configPath.encode("ascii"), weightPath.encode("ascii"), 0, 1) # batch size = 1 if metaMain is None: metaMain = load_meta(metaPath.encode("ascii")) if altNames is None: # In Python 3, the metafile default access craps out on Windows (but not Linux) # Read the names file and create a list to feed to detect try: with open(metaPath) as metaFH: metaContents = metaFH.read() import re match = re.search("names *= *(.*)$", metaContents, re.IGNORECASE | re.MULTILINE) if match: result = match.group(1) else: result = None try: if os.path.exists(result): with open(result) as namesFH: namesList = namesFH.read().strip().split("\n") altNames = [x.strip() for x in namesList] except TypeError: pass except Exception: pass if initOnly: print("Initialized detector") return None if not os.path.exists(imagePath): raise ValueError("Invalid image path `"+os.path.abspath(imagePath)+"`") # Do the detection #detections = detect(netMain, metaMain, imagePath, thresh) # if is used cv2.imread(image) detections = detect(netMain, metaMain, imagePath.encode("ascii"), thresh) if showImage: try: from skimage import io, draw import numpy as np image = io.imread(imagePath) print("*** "+str(len(detections))+" Results, color coded by confidence ***") imcaption = [] for detection in detections: label = detection[0] confidence = detection[1] pstring = label+": "+str(np.rint(100 * confidence))+"%" imcaption.append(pstring) print(pstring) bounds = detection[2] shape = image.shape # x = shape[1] # xExtent = int(x * bounds[2] / 100) # y = shape[0] # yExtent = int(y * bounds[3] / 100) yExtent = int(bounds[3]) xEntent = int(bounds[2]) # Coordinates are around the center xCoord = int(bounds[0] - bounds[2]/2) yCoord = int(bounds[1] - bounds[3]/2) boundingBox = [ [xCoord, yCoord], [xCoord, yCoord + yExtent], [xCoord + xEntent, yCoord + yExtent], [xCoord + xEntent, yCoord] ] # Wiggle it around to make a 3px border rr, cc = draw.polygon_perimeter([x[1] for x in boundingBox], [x[0] for x in boundingBox], shape= shape) rr2, cc2 = draw.polygon_perimeter([x[1] + 1 for x in boundingBox], [x[0] for x in boundingBox], shape= shape) rr3, cc3 = draw.polygon_perimeter([x[1] - 1 for x in boundingBox], [x[0] for x in boundingBox], shape= shape) rr4, cc4 = draw.polygon_perimeter([x[1] for x in boundingBox], [x[0] + 1 for x in boundingBox], shape= shape) rr5, cc5 = draw.polygon_perimeter([x[1] for x in boundingBox], [x[0] - 1 for x in boundingBox], shape= shape) boxColor = (int(255 * (1 - (confidence ** 2))), int(255 * (confidence ** 2)), 0) draw.set_color(image, (rr, cc), boxColor, alpha= 0.8) draw.set_color(image, (rr2, cc2), boxColor, alpha= 0.8) draw.set_color(image, (rr3, cc3), boxColor, alpha= 0.8) draw.set_color(image, (rr4, cc4), boxColor, alpha= 0.8) draw.set_color(image, (rr5, cc5), boxColor, alpha= 0.8) if not makeImageOnly: io.imshow(image) io.show() detections = { "detections": detections, "image": image, "caption": "\n<br/>".join(imcaption) } except Exception as e: print("Unable to show image: "+str(e)) return detections
def get_result(imagePath, netMain, metaMain, thresh= 0.25): # print(imagePath) # if not os.path.exists(imagePath): # raise ValueError("Invalid image path `"+os.path.abspath(imagePath)+"`") save_path= 'static/uploads/' filename= os.path.basename(imagePath).split('.')[0] save_filename= os.path.join(save_path, 'pred_'+filename+'.png') detections = detect(netMain, metaMain, imagePath.encode("ascii"), thresh) total_lum= 0 try: image= cv2.imread(imagePath) print("*** "+str(len(detections))+" Results, color coded by confidence ***") count_st= 1 for detection in detections: label = detection[0] confidence = detection[1] bounds = detection[2] shape = image.shape yExtent = int(bounds[3]) xEntent = int(bounds[2]) # Coordinates are around the center xCoord = int(bounds[0] - bounds[2]/2) yCoord = int(bounds[1] - bounds[3]/2) boundingBox = [ [xCoord, yCoord], [xCoord, yCoord + yExtent], [xCoord + xEntent, yCoord + yExtent], [xCoord + xEntent, yCoord] ] endX = xCoord + xEntent endY = yCoord + yExtent print(label.decode()) if label.decode() == 'Street light': luminosity= cal_luminosity(image[yCoord:endY, xCoord:endX]) total_lum+= luminosity image_save= image[yCoord:endY, xCoord:endX] # cv2.imwrite(os.path.join(save_path, 'pred_'+filename+'_st'+str(count_st)+'.png'), image_save) # count_st+=1 # # Wiggle it around to make a 3px border rr, cc = draw.polygon_perimeter([x[1] for x in boundingBox], [x[0] for x in boundingBox], shape= shape) rr2, cc2 = draw.polygon_perimeter([x[1] + 1 for x in boundingBox], [x[0] for x in boundingBox], shape= shape) rr3, cc3 = draw.polygon_perimeter([x[1] - 1 for x in boundingBox], [x[0] for x in boundingBox], shape= shape) rr4, cc4 = draw.polygon_perimeter([x[1] for x in boundingBox], [x[0] + 1 for x in boundingBox], shape= shape) rr5, cc5 = draw.polygon_perimeter([x[1] for x in boundingBox], [x[0] - 1 for x in boundingBox], shape= shape) boxColor = (int(255 * (1 - (confidence ** 2))), int(255 * (confidence ** 2)), 0) draw.set_color(image, (rr, cc), boxColor, alpha= 0.8) draw.set_color(image, (rr2, cc2), boxColor, alpha= 0.8) draw.set_color(image, (rr3, cc3), boxColor, alpha= 0.8) draw.set_color(image, (rr4, cc4), boxColor, alpha= 0.8) draw.set_color(image, (rr5, cc5), boxColor, alpha= 0.8) cv2.putText(image, label.decode(), (xCoord, yCoord), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,0,0), 2, cv2.LINE_AA) # image= cv2.cvtColor(image, cv2.COLOR_RGB2BGR) cv2.imwrite(save_filename, image) except Exception as e: print("Unable to show image: "+str(e)) return save_filename, round(total_lum, 2)
def test_polygon_perimeter_outside_image(): rr, cc = polygon_perimeter([-1, -1, 3, 3], [-1, 4, 4, -1], shape=(3, 4)) assert_equal(len(rr), 0) assert_equal(len(cc), 0)
initial_rotation = np.zeros(3) initial_scale = np.zeros(3) initial_translation = np.zeros((3, 2)) for i, wing in enumerate(wings): tform = SimilarityTransform(rotation=wing.orientation) major = wing.major_axis_length * 1.125 minor = wing.minor_axis_length * 1.125 initial_scale[i] = 2 * np.sqrt(np.power(major / 2, 2) + np.power(minor / 2, 2)) initial_rotation[i] = -wing.orientation initial_translation[i, :] = wing.centroid coords = np.array([[-(minor / 2), -(major / 2)], [-(minor / 2), (major / 2)], [(minor / 2), (major / 2)], [(minor / 2), -(major / 2)]]) rotated_coords = tform(coords) + wing.centroid box_coords = polygon_perimeter(rotated_coords[:, 0], rotated_coords[:, 1]) set_color(wings_image, box_coords, [0, 0, 1]) # write_image('distance_box.png', wings_image) slices = [slice(13, -2)] + [slice(start, None) for start in range(13)[::-1]] # slices = [slice(None)] inference = subspace_shape.infer(edges, edge_lengths, *shape_model, update_slice=slices[0], scale_estimate=initial_scale[0], rotation=initial_rotation[0], translation=initial_translation[0, [1, 0]]) fitted_shape_old = np.zeros_like(shape_model[0].reshape(-1, 2))