async def get_segmentation_map(file: bytes = File(...)): """Get segmentation maps from image file""" segmented_image = get_segments(model, file) with tempfile.NamedTemporaryFile(mode="w+b", suffix=".png", delete=False) as outfile: segmented_image.save(outfile) return FileResponse(outfile.name, media_type="image/png")
def start(image_name, output_name): image = Image.open(image_name) segments = segmentation.get_segments(image, THRESHOLD, MIN_SIZE) exp = Expressionist(segments) exp.manipulate(image.size).save(output_name) segments.sort(key = lambda s: s.size) l = len(segments)
def evaluate(datos, ponderaciones, silencios, carga_computacional=1,tipo_clustering="KMEANS", ncluster = 3, metrica = "SIL"): ''' Dado un clasificador y un conjunto de train y test devuelve su tasa de acierto en test. ''' global light_segs fecha = False if len(ponderaciones) == 0: return 0.0 ligero = np.sign(carga_computacional) < 0 carga_computacional = abs(carga_computacional) if carga_computacional < 1: datos = datos.sample(int(datos.shape[0] * carga_computacional)) datos = datos.sort_index() carga_computacional = 1 fecha = True if carga_computacional>=1: if not ligero: segmentos, _ = sg.segmentate_data_frame(df=datos, montecarlo=1, min_size=4, silence=silencios, vector_importancias=ponderaciones, verbose=False) mean = len(sg.ultra_light_segmentation(datos, fecha=fecha))/2 else: if len(light_segs) == 0: light_segs = sg.ultra_light_segmentation(datos, fecha=fecha) segmentos = light_segs mean = len(segmentos)/2 #mean = int(datos.shape[0]/100) if carga_computacional == 1: segmentados = cl.apply_segmentation(datos, segmentos, silencios, ponderaciones, fecha) if segmentados.shape[0] <= 6: return 0.0 else: std = np.sqrt(mean) nsegs=[] for i in range(segmentados.shape[0]): nsegs.append([i,i+1]) segmentos = sg.join_segments(data=segmentados, o_segments=nsegs, distance=sg.interpretable_distance, threshold=0.5, minimum_size=1,silence=silencios, vector_importancias=ponderaciones)[0] segmentados = cl.apply_segmentation(segmentados, segmentos, silencios, ponderaciones, fecha) return cl.hopkins_statistic(cl.filter_numerical(segmentados),m=int(segmentados.shape[0]*0.5))* normal_correction(mean, std, len(segmentados)) elif carga_computacional==2: if tipo_clustering == "DTW": segmentados = sg.get_segments(datos, segmentos) asignaciones = pam.kmedoids(segmentados, n_clus = ncluster) segments_df = cl.apply_clustering(datos, segmentos, asignaciones[1], asignaciones[2]) elif tipo_clustering == "KMEANS": segments_df = cl.full_clustering(datos, segmentos, n_clus = ncluster, silencio=silencios, pesos = ponderaciones, normalizar=False) if metrica == "SIL": return cl.sil_metric(segments_df[0]) elif metrica == "ENTROPY": return entropy_metric(segments_df)
rgb = cv2.cvtColor(camera_frame, cv2.COLOR_BGR2BGRA) cv2.imshow('camera_frame', rgb) if frame_time - last_frame_time > FRAME_FREQUENCY: cropped = frame[300:600, 400:900] gray_cropped = Image.fromarray(cropped).convert('L') row_avg = numpy.average(numpy.asarray(gray_cropped)) avg = numpy.average(row_avg) print avg if avg > 165: count += 1 if avg > 165 and count > 20: """ thread this! """ cv2.imwrite('capture.png', cropped) resized_images, segments, images = get_segments('capture.png') print segments segment_boxes = segments for i in xrange(len(images)): cv2.imwrite('segment-' + str(i) + '.png', numpy.asarray(images[i])) last_frame_time = frame_time count = 0 net = network.Network([784, 30, 13]) net.load_parameters() for i in resized_images: print net.recognize(i) if cv2.waitKey(1) & 0xFF == ord('q'): break
def get_segmentation_map(file: bytes = File(...)): """Get segmentation maps from image file""" segmented_image = get_segments(model, file) bytes_io = io.BytesIO() segmented_image.save(bytes_io, format="PNG") return Response(bytes_io.getvalue(), media_type="image/png")
def get_segmentation_map(file: bytes = File(...)): """Obtenemos el mapa de segmentación de la imagen""" segmented_image = get_segments(model, file) bytes_io = io.BytesIO() segmented_image.save(bytes_io, format="PNG") return Response(bytes_io.getvalue(), media_type="image/png")