def run(): # NOTE(gRPC Python Team): .close() is possible on a channel and should be # used in circumstances in which the with statement does not fit the needs # of the code. options = [('Grpc.Core.ChannelOptions.MaxMessageLength', (640 * 480 * 4 * 8))] with grpc.insecure_channel('10.10.2.2:3030', options = options) as channel: stub = image_pb2_grpc.ModelStub(channel) for i in range(30): image = image_pb2.Image(image_data = np.zeros((120,160,3)).tobytes(),width = 160, height = 120, frame = i) response = stub.predict(image) pred = response probs = np.frombuffer(pred.probs).reshape(tuple(pred.shape)) print("Recieved image size", probs.shape) time.sleep(2)
def main(pbpath): with open(pbpath, "rb") as pb: image = image_pb2.Image() image.ParseFromString(pb.read()) w = image.width h = image.height c = image.channel print("Deserialized image width :", w) print("Deserialized image height :", h) print("Deserialized image channel:", c) data = image.data img_np = np.frombuffer(data, np.uint8) img_np = img_np.reshape(h, w, c) cv2.imwrite("deserialized.png", img_np) print("Deserialized image dumped to deserialized.png.")
def index(): data = request.data # Read the existing image. image_packet = impb.Image() image_packet.ParseFromString(data) with open('picture_out_for.jpg', 'wb') as f: f.write(image_packet.image_data) f.close() img = Image.open('picture_out_for.jpg') # resize the image width, height = img.size aspect_ratio = height / width new_width = 120 new_height = aspect_ratio * new_width * 0.55 img = img.resize((new_width, int(new_height))) # new size of image # print(img.size) # convert image to greyscale format img = img.convert('L') pixels = img.getdata() # replace each pixel with a character from array chars = ["B", "S", "#", "&", "@", "$", "%", "*", "!", ":", "."] new_pixels = [chars[pixel // 25] for pixel in pixels] new_pixels = ''.join(new_pixels) # split string of chars into multiple strings of length equal to new width and create a list new_pixels_count = len(new_pixels) ascii_image = [new_pixels[index:index + new_width] for index in range(0, new_pixels_count, new_width)] ascii_image = "\n".join(ascii_image) print(ascii_image) # write to a text file. with open("ascii_image.txt", "w") as f: f.write(ascii_image) f.close() with open("ascii_image.txt", 'r') as f: file_contents = f.read() print(file_contents) f.close() return(file_contents)
def imageResize(): ''' 图片处理模块 ''' #1 解析参数,获取post过来的值 data = request.args.get('data', '') if data == '': retData = { 'retCode': 0, 'retMsg': '参数错误', 'retCode': '', } return json.dumps(data) img = image_pb2.Image() img.ParseFromString(data) url = img.url if count(image.size) < 1: retData = { 'retCode': 0, 'retMsg': '参数错误', 'retCode': '', } return json.dumps(data) return json.dumps(data) #2 下载图片 local_image = download_img(url) image_dir, image_name, image_ext = self._os_path_split(local_image) imgHandler = img_resize() for item in image.size: width = item.width height = item.height file_extra_name = item.file_extra_name pngquant_quality = item.pngquant_quality webp_quality = item.webp_quality pngquant_dest_image = os.path.join(image_dir, '%s_%dx%d_pngquant%s%s' % (image_name, width, height, file_extra_name, image_ext)) pngquant_image, webp_image = resize_compress_image(local_image, (width, height), pngquant_quality, webp_quality, pngquant_dest_image)
#!/usr/bin/env python import image_pb2 as impb import sys import os import numpy with open('008_0007.jpg', 'rb') as f: payload = f.read() #payload = bytearray(payload) image_packet = impb.Image() image_packet.image_data = payload # Write the new address book back to disk. f = open("image_data.proto.bin", "wb") f.write(image_packet.SerializeToString()) f.close()
region_1.predictions.extend([pred_11, pred_12]) rect_prop_2 = pb.RectangleProperty() rect_prop_2.upper_left_x = 2046 rect_prop_2.upper_left_y = 1739 rect_prop_2.width = 1024 rect_prop_2.height = 640 region_2 = pb.Region() region_2.shape = pb.Region.RECTANGLE region_2.rectangle_property.CopyFrom(rect_prop_2) region_2.dolphin_id = '20100702_03' region_2.group_id = 13 region_2.predictions.extend([pred_21, pred_22, pred_23]) image = pb.Image() image.url = 'http://localhost:1337/parse/files/1/ac646d1b62192a605834f6ec8793d71a_pic.jpg' image.filename = 'HL20120708_01_gg_096_IMG_2934.JPG' image.sighting = 'HL20120708_01' image.location.CopyFrom(location) image.photographer = 'Alien Lien' image.species = 'Grampus griseus' image.shot_ts = int(time.time()) image.regions.extend([region_1, region_2]) fin_image = pb.FinImage() fin_image.url = 'http://localhost:1337/parse/files/1/ac646d1b62192a605834f6ec8793d71a_fin.jpg' fin_image.dolphin_id = 'ku_007' fin_image.photo_id = pb.FinImage.LEFT fin_image.parent = 'ac646d1b62192' fin_image.predictions.extend([pred_11, pred_12])
#!/usr/bin/env/python # coding:utf-8 import image_pb2 import sys f = open('testfile', "rb") imageObj = image_pb2.Image() imageObj.ParseFromString(f.read()) f.close() imageObj.url = "http://www.baidu.com" size = imageObj.size.add() size.width = 155 size.height = 204 size.file_extra_name = '_15' size.pngquant_quality = '0-15' size.webp_quality = 50 data = imageObj.SerializeToString() print data f = open('testfile', "wb") f.write(data) ''' screenshot_resolutions = (
start = datetime.utcnow() # Collect time for benchmarks or whatever. img = Image.open(source).convert("RGBA") colormap = img.getcolors(maxcolors=args.maxcolors) default_color = (0, 0, 0, 255) if colormap: default_color = sorted(colormap, reverse=True)[0][1] default_color_message = to_color_message(default_color) data = img.load() message = image_pb2.Image() message.version = version message.size.width = img.width message.size.height = img.height message.transparency = True layer = image_pb2.Image.Layer(default_color=default_color_message) for x in range(0, img.width): for y in range(0, img.height): px = data[x, y] if default_color and default_color == px: continue pixel = image_pb2.Image.Pixel(position=image_pb2.Image.Position(x=x, y=y),
acc.append(result) elif len(acc) > 20: time_star = time.time() rest = len(acc) % 3 step = len(acc) // 3 r = reduce(add, acc[:step]) g = reduce(add, acc[step:2 * step + rest]) b = reduce(add, acc[2 * step + rest:]) star = np.uint8(normalize(np.stack([r, g, b], 2)) * 255) #star = get_starRGB(acc))*255) star = m.imresize(star, (120, 160)) time_star = time.time() - time_star time_predict = time.time() image = image_pb2.Image(image_data=star.tobytes(), width=160, height=120, frame=1) pred = model.predict(image) time_predict = time.time() - time_predict probs = np.frombuffer(pred.probs, "float32").reshape(tuple(pred.shape)) p, c = probs.max(), probs.argmax() #print("elapsed = {} prob = {} class = {}".format(time.time()-start,p,c)) image = cv2.putText(m.imresize(star,(480,640)), \ "Elapsed star + pred = {:.3f} + {:.3f} = {:.3f} (s)".format(time_star, time_predict,time_star + time_predict),\ org, font, fontScale, color, thickness, cv2.LINE_AA) image =cv2.putText(m.imresize(image,(480,640)), \ "prob = {:.2f} class = {}".format(p,classes[c]),\ (10,80), font, fontScale, color, thickness, cv2.LINE_AA) cv2.imshow('frame', m.imresize(np.hstack([frame, image]), (600, 1600))) cv2.waitKey(30) for _ in range(120):
if hasattr(dct, "default_color"): return dct.default_color elif hasattr(dct, "default_colour"): return dct.default_colour elif hasattr(dct, "color"): return dct.color elif hasattr(dct, "colour"): return dct.colour else: return image_pb2.Image.Color(red=0, green=0, blue=0, alpha=255) start = datetime.utcnow() # Collect time for benchmarks or whatever. with open(source, "rb") as inp: primg = image_pb2.Image().FromString(inp.read()) if primg.version != version: print("JSNG has invalid version!") exit() transparency = primg.transparency size = (primg.size.width, primg.size.height) base = Image.new("RGBA", size, (0, 0, 0, 0)) for l in primg.layers: layer = Image.new("RGBA", size, from_color_message(resolve_color(l))) for p in l.pixels: layer.putpixel((p.position.x, p.position.y),
import grpc import image_pb2 import image_pb2_grpc channel = grpc.insecure_channel('localhost:50051') stub = image_pb2_grpc.ImageServiceStub(channel) image = image_pb2.Image(image = open('Flatirons_Winter_Sunrise_edit_2.jpg', 'rb').read()) response = stub.ImageDimensions(image) print(response.height) print(response.widht)