Created
September 8, 2017 08:55
-
-
Save helxsz/4b6998b97869ed34c58686c506c184a3 to your computer and use it in GitHub Desktop.
annotate the parking images
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: UTF-8 -*- | |
import cv2 | |
import numpy as np | |
import os | |
import random | |
def car_position(input_file): | |
position = [] | |
img_gray = cv2.imread(input_file,cv2.IMREAD_GRAYSCALE) | |
#图片的预处理 | |
ret,thresh = cv2.threshold(img_gray,254,255,cv2.THRESH_BINARY) #对图片进行二值化处理 | |
kernel = np.array(cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3), (-1, -1))) #内核是3x3的全1矩阵,(-1,-1)锚位于中心 | |
img_open = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel) #先膨胀后腐蚀,开运算 | |
#img_close = cv2.morphologyEx(thresh1, cv2.MORPH_CLOSE, kernel) | |
ret1,thresh1 = cv2.threshold(img_open,254,255,cv2.THRESH_BINARY_INV) #再次对图片进行二值化反转处理,为了不把整张图片框住 | |
contours, hierarchy = cv2.findContours(thresh1, cv2.RETR_CCOMP ,cv2.CHAIN_APPROX_NONE) | |
for i in range(len(contours)): | |
if len(contours[i]) > 30: | |
x, y, w, h = cv2.boundingRect(contours[i]) | |
#cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2) | |
#print (x, y),(x+w, y+h) | |
position.append([x, y, x+w, y+h]) | |
return position | |
def readFolderImages(input_dir, file_name): | |
doc = open(input_dir+ file_name +"/position.txt",'w') | |
print(input_dir+ file_name +"/position.txt") | |
for root, dirs, files in os.walk(input_dir+ file_name + "/images2", topdown=True): | |
for name in files: | |
print root, name | |
doc.write(os.path.join(root,name).replace("0001","").replace("images2","images")) | |
input_file = os.path.join(root,name) | |
position = car_position(input_file) | |
doc.write(" "+ str(len(position)) ) | |
for i in range(len(position)): | |
for j in range(len(position[i])): | |
position_point = position[i][j] | |
doc.write(" "+ str(position_point)) | |
doc.write(" \n") | |
doc.close() | |
if __name__=="__main__": | |
folders = [ | |
"20","25","35","45","55","60","65","75","85","(30,30)", | |
"(30,45)","(30,60)","(45,30)","(45,45)","(45,60)","(60,30)", | |
"(60,45)","(60,60)", | |
"parking_line_3","parking_line_30","parking_line_40","parking_line_50","parking_line_60", | |
"25_change_22", "(30,75)1","25_change11","30_75","88888","60_30_3","60_30_2","60_30_1" | |
] | |
input_dir = os.path.dirname(os.path.abspath(__file__))+"/JPEGImages/" | |
for folder in folders: | |
print(folder) | |
readFolderImages(input_dir ,folder) | |
annotation_file = os.path.dirname(os.path.abspath(__file__))+"/Annotations/annotation.txt" | |
training_file = os.path.dirname(os.path.abspath(__file__))+"/ImageSets/train.txt" | |
testing_file = os.path.dirname(os.path.abspath(__file__))+"/ImageSets/test.txt" | |
annotation_doc = open(annotation_file,'w') | |
training_doc = open(training_file,'w') | |
testing_doc = open(testing_file,'w') | |
for folder in folders: | |
with open(input_dir+ folder +"/position.txt",'r') as f: | |
for line in f.readlines(): | |
path = line.split(" ")[0] | |
if(random.random() < 0.9): | |
training_doc.write(path+"\n") | |
annotation_doc.write(line) | |
else: | |
testing_doc.write(path+"\n") | |
annotation_doc.close() | |
training_doc.close() | |
testing_doc.close() | |
#img_output = np.ones(img_gray.shape,dtype = np.uint8)*255 | |
#cv2.drawContours(img,contours,-1,(0,0,255),3) | |
''' | |
cv2.imshow('img_open',thresh) | |
cv2.waitKey(0) | |
cv2.destroyAllWindows() | |
''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment