def calculate_communities(self): """ Calculates communities and assigns a color per community """ if self.num_communities is -1: mod = self.graph.community_multilevel(weights='weight') self.graph.vs['cluster'] = mod self.num_communities = len(mod) pal = ClusterColoringPalette(len(mod)) self.graph.vs['cluster_color'] = list( map(lambda x: rgb2hex(x[0], x[1], x[2], normalised=True), pal.get_many(mod.membership)))
def calculate_communities(self): """ Calculates communities and assigns a color per community """ if not 'n_communities' in self.graph.attributes(): weight = 'weight' if 'weight' in self.graph.es.attributes( ) else None # igraph bug: https://github.com/igraph/python-igraph/issues/17 try: v_d = self.graph.community_walktrap(weights=weight, steps=6) mod = v_d.as_clustering() except: fix_dendrogram(self.graph, v_d) mod = v_d.as_clustering() self.graph.vs['cluster'] = mod.membership self.graph['n_communities'] = len(mod) pal = ClusterColoringPalette(len(mod)) self.graph.vs['cluster_color'] = list(map(lambda x: rgb2hex(x[0],x[1],x[2],\ normalised=True), pal.get_many(mod.membership)))
def create(self, request): try: # TEXT_DETECTION FROM IMAGE encoded_string = (request.data).get('base64') data = { "requests": [{ "image": { "content": encoded_string }, "features": [{ "type": "TEXT_DETECTION" }] }] } data = json.dumps(data) r = requests.post( "https://vision.googleapis.com/v1/images:annotate?key=AIzaSyCNHAv4uQDW2JJmaERfoCvrGVipxr_moww", data=data) response = json.loads(r.text) text_of_image = response['responses'][0]['textAnnotations'][0][ 'description'] # DETECT LOGO FROM IMAGE data = { "requests": [{ "image": { "content": encoded_string }, "features": [{ "type": "LOGO_DETECTION" }] }] } data = json.dumps(data) r = requests.post( "https://vision.googleapis.com/v1/images:annotate?key=AIzaSyCNHAv4uQDW2JJmaERfoCvrGVipxr_moww", data=data) response = json.loads(r.text) vertices = response['responses'][0]['logoAnnotations'][0][ 'boundingPoly']['vertices'] first_vertices = vertices[0] third_vertices = vertices[2] x1 = first_vertices['x'] y1 = first_vertices['y'] x2 = third_vertices['x'] y2 = third_vertices['y'] b = bytes(encoded_string, 'utf-8') image_64_decode = base64.decodestring(b) image_result = open(BASE_DIR + "croping.png", 'wb') image_result.write(image_64_decode) img = Image.open(BASE_DIR + "croping.png") img2 = img.crop((x1, y1, x2, y2)) img2.save(BASE_DIR + "img2.png") # Here convert logo with base64 string with open(BASE_DIR + "img2.png", "rb") as logo_file: encoded_logo = base64.b64encode(logo_file.read()) encoded_logo = encoded_logo.decode('utf-8') # DETECT COLOR FROM IMAGE data = { "requests": [{ "image": { "content": encoded_string }, "features": [{ "type": "IMAGE_PROPERTIES" }] }] } data = json.dumps(data) r = requests.post( "https://vision.googleapis.com/v1/images:annotate?key=AIzaSyCNHAv4uQDW2JJmaERfoCvrGVipxr_moww", data=data) response = json.loads(r.text) color_list = response['responses'][0]['imagePropertiesAnnotation'][ 'dominantColors']['colors'] length = len(color_list) modify_dict = {} for i in range(0, length): obj = color_list[i] pixelFraction_value = obj['pixelFraction'] modify_dict[pixelFraction_value] = { 'color': obj['color'], 'score': obj['score'] } pixels = list(modify_dict.keys()) max_value = max(pixels) max_pixel = modify_dict.get(max_value) color_dict = max_pixel['color'] final_value = rgb2hex(color_dict['red'], color_dict['blue'], color_dict['green']) final_json = { "text": text_of_image, "Logo_with_base64": encoded_logo, "background_color": final_value } return Response(final_json, status=status.HTTP_201_CREATED) except Exception as error: now = datetime.datetime.now() error = str(now) + " ====> " + str(error) logging.debug(error) return Response( "Please check your image it does not have text/logo", status=status.HTTP_400_BAD_REQUEST)
def test_rgb2hex(): colors.rgb2hex(0,0,255) colors.rgb2hex(0,0,1) colors.rgb2hex(*(0,0,1)) try: colors.rgb2hex([0,0]) assert False except: assert True try: colors.rgb2hex(0,0,1000) assert False except: assert True try: colors.rgb2hex(0,0,-1000) assert False except: assert True try: colors.rgb2hex(0,0,10, normalised=True) assert False except: assert True
def test_rgb2hex(): colors.rgb2hex(0, 0, 255) colors.rgb2hex(0, 0, 1) colors.rgb2hex(*(0, 0, 1)) try: colors.rgb2hex([0, 0]) assert False except: assert True try: colors.rgb2hex(0, 0, 1000) assert False except: assert True try: colors.rgb2hex(0, 0, -1000) assert False except: assert True try: colors.rgb2hex(0, 0, 10, normalised=True) assert False except: assert True