代码在本地运行,但不在服务器上运行(Django、Opencv)

时间:2021-04-05 06:51:44

标签: python django opencv

我正在尝试使用 cv2.imwrite 函数转换图像并将其临时保存在静态文件夹中,但是我收到一个错误消息,显示 No such file or directory: './static/images/0.png'(为了转换图像,我不得不暂时保存图像)。它在本地工作,但不在服务器上。我该怎么办?

    class Article(models.Model): 
    writer = models.ForeignKey(User, on_delete=models.SET_NULL, related_name='article', null=True)
    project = models.ForeignKey(Project, on_delete=models.SET_NULL, related_name='article', null=True)
    title = models.CharField(max_length=200, null =True)
    image = models.ImageField(upload_to='article/', null=True, blank=True)
    image_converted = models.ImageField(upload_to='article/converted/', null=True, blank=True)
    style = models.CharField(max_length = 50, blank = True, null=True, choices=ACTION_CHOICES)
    content = models.TextField(null=True)
    created_at = models.DateField(auto_now_add=True, null=True)
    like = models.IntegerField(default=0)


    def convert_image(self, *args, **kwargs):
        image_converted = convert_rbk(self.image, self.style)
        self.image_converted = InMemoryUploadedFile(file=image_converted, 
                                                    field_name="ImageField", 
                                                    name=self.image.name, 
                                                    content_type='image/png', 
                                                    size=sys.getsizeof(image_converted), 
                                                    charset=None)

    def convert_rbk(img, style):
    if style == "HAYAO":
        img = Image.open(img)
        img = img.convert('RGB')
        img = ImageOps.exif_transpose(img)
        img.save("./static/images/0.png")


        model = Transformer()
        model.load_state_dict(torch.load('pretrained_model/Hayao_net_G_float.pth'))
        model.eval()

        img_size = 450
        img = cv2.imread('./static/images/0.png')

        T = transforms.Compose([
            transforms.ToPILImage(),
            transforms.Resize(img_size, 2),
            transforms.ToTensor()
        ])

        img_input = T(img).unsqueeze(0)

        img_input = -1 + 2 * img_input

        img_output = model(img_input)

        img_output = (img_output.squeeze().detach().numpy() + 1.) /2.
        img_output = img_output.transpose([1,2,0])
        img_output = cv2.convertScaleAbs(img_output, alpha = (255.0)) 
        cv2.imwrite('./static/images/1.png', img_output) 

        result_image = "./static/images/2.png"
        cmd_rembg = "cat " + "./static/images/0.png"  + " | python3 ./remvbk.py > " + result_image
        os.system(cmd_rembg)

        
        src1 = cv2.imread("./static/images/2.png", cv2.IMREAD_UNCHANGED)  
        src = cv2.imread("./static/images/1.png", cv2.IMREAD_COLOR)        
        h, w = img.shape[:2]    
        h1, w1 = src1.shape[:2]    

        src = cv2.resize(src, dsize=(w, h), interpolation=cv2.INTER_AREA)
        for y in range (h):
            for x in range(w):
                img[y,x] = 255

        mask = src1[:, :, -1] #(1440, 666)
        th, mask1 = cv2.threshold(mask, 2, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
        mask1 = cv2.resize(mask1, dsize=(w,h), interpolation=cv2.INTER_AREA )

        i = "./static/images/3.png" 
        cv2.imwrite(i, mask1)

        j = "./static/images/4.png" 
        cv2.copyTo(src, mask1, img)
        cv2.imwrite(j, img)

        k = "./static/images/5.png"
        cmd_rembg1 = "cat " + j  + " | python3 ./remvbk.py > " + k
        os.system(cmd_rembg1)
        img = Image.open(k)
        os.remove(i)  
        os.remove(j)
        os.remove(k)
        os.remove('./static/images/1.png')
        os.remove('./static/images/2.png')
        os.remove('./static/images/0.png')
        return image_to_bytes(img)

0 个答案:

没有答案