def my_stitch_multiple(images, ratio=0.75, reprojThresh=4.0, affine=False): firstLoop = True first_image = Image(None, None) second_image = Image(None, None) temp_image = Image(None, None) for i in range(len(images)): if firstLoop: first_image = images[i] second_image = images[i + 1] firstLoop = False else: first_image = temp_image second_image = images[i] try: temp_image, left_image, homograph = stitch_two( first_image, second_image, affine=affine, ratio=ratio, reprojThresh=reprojThresh) temp_image.image = fix_distortion(temp_image.image, strength=0.41) temp_image.image = _crop_black(temp_image.image) print("Progress: " + str(i) + "/" + str(len(images) - 1)) except: print("Error: " + str(i) + "/" + str(len(images) - 1) + ", with image " + str(images[i])) output_image_name = "stitched_img_.png" temp_image.save(path="/home/hduser/dev-materials/", filename=output_image_name) return temp_image
def warp_perspective(img1, img2, H, affine=False, skip=False): # Gets the width and height of the current images for future point # reading use h1, w1 = img1.image.shape[:2] h2, w2 = img2.image.shape[:2] # Convert the top left, bottom left, bottom right, top left # (respectively) points of the image into numpy arrays pts1 = np.float32([[0, 0], [0, h1], [w1, h1], [w1, 0]]).reshape(-1, 1, 2) pts2 = np.float32([[0, 0], [0, h2], [w2, h2], [w2, 0]]).reshape(-1, 1, 2) # Find dimensions of the transformed right image with a homograph for # things to be the correct size for the future warp pts2_ = cv2.perspectiveTransform(pts2, H) # If the user wants an affine transformation (removes warping and # keeps parellel lines parellel), remove the bottom line of the # homograph. This is the quickest method to both warp the photo to the # best of SURF abilities and still keep it affine if affine: [[a, b, c], [d, e, f], [g, h, i]] = H H = [[a, b, c], [d, e, f], [0, 0, 1]] pts = np.concatenate((pts1, pts2_), axis=0) [xmin, ymin] = np.int32(pts.min(axis=0).ravel() - 0.5) [xmax, ymax] = np.int32(pts.max(axis=0).ravel() + 0.5) t = [-xmin, -ymin] # The homograph that will move the image around with the given # homograph so that no edges are cropped off, and most of the black # border is gone Ht = np.array([[1, 0, t[0]], [0, 1, t[1]], [0, 0, 1]]) # translate # Using the calculated homograph and the given homograph, warp the img result = Image(None, None) if skip: result.image = img2.image else: result.image = cv2.warpPerspective(img2.image, Ht.dot(H), (xmax - xmin, ymax - ymin)) # Place the unwarped photo over top of the warped one. You now have a # stitched image! result.image[t[1]:h1 + t[1], t[0]:w1 + t[0]] = overlay_image( result.image[t[1]:h1 + t[1], t[0]:w1 + t[0]], img1.image) # Update the unwarped image's coordinates, since it is now a part of # the stitched picture img1.place_image(t[0], t[1], w1 + t[0], h1 + t[1]) # Return the stitched image, the image that was placed on top of the # result (with an updated current coordinates), and the homograph for # bookkeeping reasons return result, img1, Ht.dot(H)
def upload(request): if request.method == 'POST': data = request.POST.get('img') u_id = request.POST.get('uid') g_id = request.POST.get('gid') buf = StringIO.StringIO() for line in data.splitlines(): line = line.strip().replace(" ", "") if not line: continue bytes = binascii.unhexlify(line) buf.write(bytes) path = str(g_id) + str(u_id) + ".jpg" with open(path, "wb") as f: f.write(buf.getvalue()) img = Image() img.image = path img.save() text = pytesseract.image_to_string(Image.open(path)) texts = text.split('\n') items = {} for t in texts: m = re.search("\w", t.reverse()) if m and m.start() == 0: price = t[m.start():] item = t[:m.start()] items[item] = price return JsonResponse(items)
def draw_matches(left_image, right_image, left_key_points, right_key_points, matches, status): # Shows all the little lines between the two pictures that were matches # Plays connect the dots with green lines and the keypoints (left_height, left_width) = left_image.shape[:2] (right_height, right_width) = right_image.shape[:2] vis = np.zeros( (max(left_height, right_height), left_width + right_width, 3), dtype="uint8") vis[0:left_height, 0:left_width] = left_image vis[0:right_height, left_width:] = right_image # loop over the matches for ((trainIdx, queryIdx), s) in zip(matches, status): # Only process the match if the keypoint was successfully matched if s == 1: # Draw the match left_point = (int(left_key_points[queryIdx][0]), int(left_key_points[queryIdx][1])) right_point = (int(right_key_points[trainIdx][0]) + left_width, int(right_key_points[trainIdx][1])) cv2.line(vis, left_point, right_point, (0, 255, 0), 1) # return the visualization img_vis = Image(None, None) img_vis.image = vis return img_vis
def post(self, request, *args, **kwargs): post = request.POST image_data = request.FILES print("\n\n\n\n\n") # print(post) # print(image_data['image']) dynamic_fields = {} try: delete = post['delete'] Item.objects.get(id=kwargs['id']).delete() return redirect('adminview') except Exception as e: print(e) # print(post) for key, value in post.items(): # print(value) if key not in [ 'name', 'brand', 'price', 'stock', 'category', 'engine', 'csrfmiddlewaretoken' ] and value != '': print("{}:{} \n".format(key, value)) dynamic_fields[key] = value name = post['name'] brand = post['brand'] price = post['price'] stock = post['stock'] category = post['category'] category = Category.objects.filter(name__icontains=category).first() data = dynamic_fields new_item = Item.objects.get(id=kwargs['id']) new_item.name = name new_item.brand = brand new_item.price = price new_item.stock = stock new_item.category = category new_item.data = data new_item.save() # for image in images: if 'image' in image_data: new_image = Image() new_image.image = image_data['image'] new_image.item = new_item new_image.save() context = {} context['categories'] = Category.objects.all() context['item'] = Item.objects.get(id=kwargs['id']) return render(request, self.template_name, context)
def post(self, request, *args, **kwargs): post = request.POST image_data = request.FILES print("\n\n\n\n\n") # print(post) # print(image_data['image']) dynamic_fields = {} # print(post) for key, value in post.items(): # print(value) if key not in [ 'name', 'brand', 'price', 'stock', 'category', 'engine', 'csrfmiddlewaretoken' ] and value != '': print("{}:{} \n".format(key, value)) dynamic_fields[key] = value name = post['name'] brand = post['brand'] price = post['price'] stock = post['stock'] category = post['category'] category = Category.objects.filter(name__icontains=category).first() data = dynamic_fields new_item = Item() new_item.name = name new_item.brand = brand new_item.price = price new_item.stock = stock new_item.category = category new_item.data = data new_item.save() # for image in images: new_image = Image() new_image.image = image_data['image'] new_image.item = new_item new_image.save() return render(request, self.template_name, {})
def crop(self, face): """ Return a new Image cropped to the face """ result = Image(self.name) result.image = self.image.crop(face.as_box()) return result
def clone(self): new = Image(self.width, self.height) new.image = self.image.copy() return new