简体   繁体   English

有没有办法使用python重命名restAPI本地服务器中的图像?

[英]Is there a way to rename images in restAPI local server using python?

i created a localhost api to analyis images and compare them (computer vision) project!我创建了一个 localhost api 来分析图像并比较它们(计算机视觉)项目!

my plan is to upload images from my data folder to the server, each image file in folder is named (fake_name.jpg/jpeg) i am trying to add the file name as a person name in parameters but can only do it manually and for each file.我的计划是将图像从我的数据文件夹上传到服务器,文件夹中的每个图像文件都被命名为 (fake_name.jpg/jpeg) 我试图将文件名添加为参数中的人名,但只能手动完成每个文件。 i am also trying to figure out how to upload multiple files.我也想弄清楚如何上传多个文件。


    def image_to_base64(self, img):
        # convert image to base64
        prependInfo = 'data:image/jpeg;base64,'
        encodedString = base64.b64encode(img).decode("utf-8")
        fullString = str(prependInfo) + encodedString
        return str(fullString)

    # the following part is to create entry in database:

    def create_person_entry(self,img):
        base_url = "localhost:8080/service/api/person/create?"

        parameters = {
            "person-name": 'homer simson' #manual change name from here before each upload
        }
        data = {
            "image-data": self.image_to_base64(img)
        }
        r = requests.post(base_url+urllib.parse.urlencode(parameters),headers{'Authorization':self.auth_tok}, data=data).json()
        return r



#to import 1 image i used:
#with open("///data/homer simson.jpg", "rb") as img:
    person_name = cvis.create_person(img.read())
    print (person_name)

it uploads successfuly but i have to manualy name the person entry from parameters "person-name" for each person i upload!它上传成功,但我必须为我上传的每个人手动命名参数“人名”中的人名! researched everywhere to automate solution!到处研究以自动化解决方案!

edit1:编辑1:

i managed to get this code working and it worked我设法让这段代码工作并且工作正常

# to upload multiple images 
#folder with JPEG/JPG files to upload 
folder = "/home///data/" 
#dict for files 
upload_list = [] 
for files in os.listdir(folder): with open("{folder}{name}".format(folder=folder, name=files), "rb") as data:
upload_list.append(files) 
person_name = cvis.create_person(data.read()) 
print (person_name) 

i managed to upload all images from directory to server it worked but now all my files are named homer simpson :)我设法将所有图像从目录上传到它工作的服务器,但现在我所有的文件都命名为 homer simpson :)

i finally managed to get this right at the suggestion made by AKX his solution is below plz upvote, thanks我终于在 AKX 提出的建议下设法做到了这一点,他的解决方案低于 plz upvote,谢谢

Now i need to figure out how to delete the previous no name entries.. will check API documentation.现在我需要弄清楚如何删除以前的无名称条目.. 将检查 API 文档。

Am I missing something – why not just add another argument to your create_person_entry() function?我是否遗漏了什么——为什么不向create_person_entry()函数添加另一个参数?


    def create_person_entry(self, name, img):
        parameters = {
            "person-name": name,
        }
        # ...
        return r

# ...

cvis.create_person_entry("homer_simpson", img.read())

And if you have a folderful of images,如果你有一大堆图片,

import os
import glob

for filename in glob.glob("people/*.jpg"):
    file_basename = os.path.splitext(os.path.basename(filename))[0]
    with open(filename, "rb") as img:
        cvis.create_person_entry(file_basename, img.read())

will use the file's name sans extension, eg people/homer_simpson.jpg is homer_simpson .将使用文件名 sans 扩展名,例如people/homer_simpson.jpghomer_simpson

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM