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)
解决方案
推荐阅读
- c++ - CERN ROOT:使用自定义类的向量分支保存到文件树中
- r - 具有变化的颜色和文本注释的动画量规图
- javascript - 动态更改表单操作不适用于 MailChimp
- regex - 正则表达式查找日期字符串
- javascript - 从数组创建数组
- html - 您如何获得标点符号以使用 RTL 语言?
- php - 启用 extension=php_fileinfo.dll 时,Laravel 无法猜测 MIME 类型?
- javascript - 如何减少javascript中的if语句?
- c# - 我正在尝试为我的列表框找到不相等的随机 6 个数字
- javascript - 按日期变量过滤 Graphql 结果