python文件处理-将图像根据坐标画矩形标记

内容涉及:文件遍历,选取csv后缀文件,用cv操作图片

import csv
import os
import sys
import numpy as np
import copy
import shutil
import pandas as pd
from collections import Counter
from shutil import copyfile
import cv2

path = path = os.getcwd()
path_1 = path + '/' + 'data_error_0813'
list_name = os.listdir(path_1)
for n in list_name:
    if n[-3:] == 'csv':
        csvpath = path_1 + '/' + n
        imgpath = path_1 + '/' + n[:-3] + 'JPG'
        print(imgpath)
        if not os.path.exists(imgpath):
            print("nothing")

        filehand = open(csvpath,'r')
        csvlist = filehand.readlines()
        mark = []
        image = []
        count = 1


        for m in csvlist[1:]:
            m_split = m.split(',')
            xy = [m_split[2], m_split[3]]
            mark.append(xy)
            image = cv2.imread(imgpath)
            print("type:",type(image))
            first_point = (int(m_split[2])-50,int(m_split[3])-50)
            last_point = (int(m_split[2])+50,int(m_split[3])+50)
            cv2.rectangle(image, first_point, last_point, (0,255,0),2)
            cv2.imwrite(imgpath,image)
            print("标记次数",count)
            count = count + 1

    else:
        continue
    print(mark)

 

posted @ 2019-08-14 10:54  牛郎  阅读(3214)  评论(0编辑  收藏  举报