最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python之json文件轉(zhuǎn)xml文件案例講解

 更新時(shí)間:2021年08月06日 09:05:06   作者:G果  
這篇文章主要介紹了python之json文件轉(zhuǎn)xml文件案例講解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下

json文件格式

這是yolov4模型跑出來的檢測結(jié)果result.json

在這里插入圖片描述

下面是截取的一張圖的檢測結(jié)果

{
 "frame_id":1, #圖片的序號
 "filename":"/media/wuzhou/Gap/rgb-piglet/test/00000000.jpg", #圖片的路徑
 "objects": [ #該圖中所有的目標(biāo):目標(biāo)類別、目標(biāo)名稱、歸一化的框的坐標(biāo)(xywh格式)、置信度
  {"class_id":0, "name":"pp", "relative_coordinates":{"center_x":0.750913, "center_y":0.402691, "width":0.038380, "height":0.193304}, "confidence":0.995435}, 
  {"class_id":0, "name":"pp", "relative_coordinates":{"center_x":0.764775, "center_y":0.199255, "width":0.049979, "height":0.130169}, "confidence":0.994495}, 
  {"class_id":0, "name":"pp", "relative_coordinates":{"center_x":0.560050, "center_y":0.482614, "width":0.036331, "height":0.166377}, "confidence":0.994460}, 
  {"class_id":0, "name":"pp", "relative_coordinates":{"center_x":0.710756, "center_y":0.406446, "width":0.041782, "height":0.191297}, "confidence":0.993540}, 
  {"class_id":0, "name":"pp", "relative_coordinates":{"center_x":0.638335, "center_y":0.238725, "width":0.107689, "height":0.092282}, "confidence":0.992926}, 
  {"class_id":0, "name":"pp", "relative_coordinates":{"center_x":0.780232, "center_y":0.448454, "width":0.041550, "height":0.179540}, "confidence":0.990020}, 
  {"class_id":0, "name":"pp", "relative_coordinates":{"center_x":0.563412, "center_y":0.350035, "width":0.103184, "height":0.059460}, "confidence":0.979756}, 
  {"class_id":0, "name":"pp", "relative_coordinates":{"center_x":0.522591, "center_y":0.195170, "width":0.083014, "height":0.071478}, "confidence":0.970642}, 
  {"class_id":0, "name":"pp", "relative_coordinates":{"center_x":0.658721, "center_y":0.154640, "width":0.103852, "height":0.055686}, "confidence":0.967082}, 
  {"class_id":0, "name":"pp", "relative_coordinates":{"center_x":0.537660, "center_y":0.256810, "width":0.101619, "height":0.095211}, "confidence":0.918135}, 
  {"class_id":0, "name":"pp", "relative_coordinates":{"center_x":0.528618, "center_y":0.481005, "width":0.033226, "height":0.177723}, "confidence":0.310291}
 ] 
}, 

完整代碼

代碼需要指定圖片的路徑,例如 file_dir = "H:/rgb-piglet/five/test"
注意:result.json文件要跟圖片放一起

代碼生成的xml與圖片在同一個(gè)路徑下

import json
import time
import os
from PIL import Image
import cv2
import numpy as np

'''人為構(gòu)造xml文件的格式'''
out0 ='''<annotation>
    <folder>%(folder)s</folder>
    <filename>%(name)s</filename>
    <path>%(path)s</path>
    <source>
        <database>None</database>
    </source>
    <size>
        <width>%(width)d</width>
        <height>%(height)d</height>
        <depth>3</depth>
    </size>
    <segmented>0</segmented>
'''
out1 = '''    <object>
        <name>%(class)s</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
            <xmin>%(xmin)d</xmin>
            <ymin>%(ymin)d</ymin>
            <xmax>%(xmax)d</xmax>
            <ymax>%(ymax)d</ymax>
        </bndbox>
    </object>
'''

out2 = '''</annotation>
'''

def read_json(json_dir):
    with open(json_dir,"r") as f:
        data = json.load(f)
        print(type(data),len(data),type(data[0]),data[0]['frame_id'])
    return data


'''txt轉(zhuǎn)xml函數(shù)'''
def translate(fdir,lists): 
    source = {}
    label = {}
    data = read_json(fdir+"/result.json")
    k = 0
    for jpg in lists:
        print(jpg)
        if jpg[-4:] == '.jpg':
            image= cv2.imread(jpg)#路徑不能有中文
            h,w,_ = image.shape #圖片大小
            
            fxml = jpg.replace('.jpg','.xml')
            fxml = open(fxml, 'w');
            imgfile = jpg.split('/')[-1]
            source['name'] = imgfile 
            source['path'] = jpg
            source['folder'] = os.path.basename(fdir)

            source['width'] = w
            source['height'] = h
            
            fxml.write(out0 % source)
                       
            for obj in data[k]["objects"]:
                label['class'] = obj["class_id"]
                box = obj["relative_coordinates"]
                
                '''把txt上的數(shù)字(歸一化)轉(zhuǎn)成xml上框的坐標(biāo)'''
                xmin = float(box["center_x"] - 0.5*box["width"])*w
                ymin = float(box["center_y"] - 0.5*box["height"])*h
                xmax = float(xmin + box["width"]*w)
                ymax = float(ymin + box["height"]*h)
                
                label['xmin'] = xmin
                label['ymin'] = ymin
                label['xmax'] = xmax
                label['ymax'] = ymax
                    
                fxml.write(out1 % label)
                
            k = k+1
            fxml.write(out2)

if __name__ == '__main__':
    file_dir = "H:/rgb-piglet/five/test"
    lists=[]
    for i in os.listdir(file_dir):
        if i[-3:]=='jpg':
            lists.append(file_dir+'/'+i)       
    #print(lists)
    translate(file_dir,lists)
    print('---------------Done!!!--------------')            
                

到此這篇關(guān)于python之json文件轉(zhuǎn)xml文件案例講解的文章就介紹到這了,更多相關(guān)python之json文件轉(zhuǎn)xml內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解OpenCV圖像的概念和基本操作

    詳解OpenCV圖像的概念和基本操作

    opencv最主要的的功能是用于圖像處理,所以圖像的概念貫穿了整個(gè)opencv,與其相關(guān)的核心類就是Mat。這篇文章主要介紹了OpenCV圖像的概念和基本操作,需要的朋友可以參考下
    2021-10-10
  • python3多線程知識點(diǎn)總結(jié)

    python3多線程知識點(diǎn)總結(jié)

    在本篇文章里小編給各位分享的是關(guān)于python3多線程的相關(guān)知識點(diǎn)內(nèi)容,以后需要的朋友們可以參考下。
    2019-09-09
  • 利用Python連接Oracle數(shù)據(jù)庫的基本操作指南

    利用Python連接Oracle數(shù)據(jù)庫的基本操作指南

    由于之前的在職的公司沒有機(jī)會接觸到Oralce數(shù)據(jù)庫,所以就沒有用python連接過Oralce,之前大多集中在連接mysql和sql server,最近在做一下web自動化的工作,所以簡單的記錄一下,下面這篇文章主要給大家介紹了關(guān)于利用Python連接Oracle數(shù)據(jù)庫的基本操作,需要的朋友可以參考下
    2022-06-06
  • python DataFrame轉(zhuǎn)dict字典過程詳解

    python DataFrame轉(zhuǎn)dict字典過程詳解

    這篇文章主要介紹了python DataFrame轉(zhuǎn)dict字典過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • 教你用一行Python代碼實(shí)現(xiàn)GUI圖形界面

    教你用一行Python代碼實(shí)現(xiàn)GUI圖形界面

    這篇文章主要介紹了教你用一行Python代碼實(shí)現(xiàn)GUI圖形界面,通過使用PySimpleGUI的popup_get_folder()方法,一行代碼就能實(shí)現(xiàn)選擇文件夾的操作,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2023-01-01
  • Python中threading庫實(shí)現(xiàn)線程鎖與釋放鎖

    Python中threading庫實(shí)現(xiàn)線程鎖與釋放鎖

    threading用于提供線程相關(guān)的操作,為了保證安全的訪問一個(gè)資源對象,我們需要?jiǎng)?chuàng)建鎖。那么Python線程鎖與釋放鎖如何實(shí)現(xiàn),感興趣的小伙伴們可以參考一下
    2021-05-05
  • Python enumerate函數(shù)遍歷數(shù)據(jù)對象組合過程解析

    Python enumerate函數(shù)遍歷數(shù)據(jù)對象組合過程解析

    這篇文章主要介紹了Python enumerate函數(shù)遍歷數(shù)據(jù)對象組合過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • Matplotlib快速入門指南(適合小白)

    Matplotlib快速入門指南(適合小白)

    這篇文章主要給大家介紹了關(guān)于Matplotlib快速入門指南的相關(guān)資料,Matplotlib是一個(gè)非常強(qiáng)大的Python畫圖工具,支持跨平臺運(yùn)行,它不僅是Python常用的2D繪圖庫,同時(shí)它也提供了一部分3D繪圖接口,需要的朋友可以參考下
    2023-09-09
  • Numpy 多維數(shù)據(jù)數(shù)組的實(shí)現(xiàn)

    Numpy 多維數(shù)據(jù)數(shù)組的實(shí)現(xiàn)

    這篇文章主要介紹了Numpy 多維數(shù)據(jù)數(shù)組的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • python3 pygame實(shí)現(xiàn)接小球游戲

    python3 pygame實(shí)現(xiàn)接小球游戲

    這篇文章主要為大家詳細(xì)介紹了python3 pygame實(shí)現(xiàn)接小球游戲,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-05-05

最新評論

瑞昌市| 奉化市| 安化县| 乐至县| 荆门市| 斗六市| 大悟县| 巧家县| 敖汉旗| 东港市| 滕州市| 元阳县| 肥西县| 衡南县| 丰都县| 建平县| 宁波市| 启东市| 临沂市| 昌乐县| 岳西县| 沁源县| 三都| 黔南| 灵川县| 平南县| 马尔康县| 谢通门县| 惠水县| 衡阳县| 万年县| 兴安盟| 印江| 资阳市| 凤阳县| 沙田区| 浪卡子县| 辰溪县| 兖州市| 新巴尔虎左旗| 靖江市|