Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # import the necessary packages
- from tkinter import W
- import cv2
- import os, os.path
- import numpy as np
- # debug info OpenCV version
- print("OpenCV version: " + cv2.__version__)
- # image path and valid extensions
- imageDir = "/home/eden/data/VOCdevkit/VOC2007/JPEGImages" # specify your path here
- image_path_list = []
- valid_image_extensions = [".jpg", ".jpeg", ".png", ".tif", ".tiff"] # specify your valid extensions here
- valid_image_extensions = [item.lower() for item in valid_image_extensions] # sorting endings
- # create a list of all the images in directory and
- # append images with a vaild extention to image_path_list
- for file in os.listdir(imageDir):
- extension = os.path.splitext(file)[1]
- if extension.lower() not in valid_image_extensions:
- continue
- image_path_list.append(os.path.join(imageDir, file))
- image_path_list = sorted(image_path_list)
- # loop through image_path_list to open each image
- for imagePath in image_path_list:
- image = cv2.imread(imagePath)
- # resize the image blur it and convert it to HSV color space
- # image =imutils.resize(image, width=500, height=375)
- blurred = cv2.GaussianBlur(image, (5, 5), 0)
- hsv = cv2.cvtColor(image, cv2.COLOR_RGB2HSV)
- # define the lower and upper boundaries in the HSV coordiantes.
- lower = np.array([90, 85, 0])
- upper = np.array([120, 255, 255])
- # define a filtering kernel
- kernel = np.ones((5, 5), np.uint8)
- # construct a mask for the desired color range, then perform a series of
- # erosions and dilations (or opening and closing) to remove any small noise left
- mask = cv2.inRange(hsv, lower, upper)
- mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel) # opening
- mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel) # closing
- res = cv2.bitwise_and(image, image, mask=mask)
- # find contours in the mask and initialize the current
- # (x, y) center of the objects
- cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
- cv2.CHAIN_APPROX_SIMPLE)[-2]
- center = None
- # only proceed if at least one contour was found
- if len(cnts) > 0:
- for i in range(len(cnts)):
- x, y, w, h = cv2.boundingRect(cnts[i]) # retrieves a bounding rectangle pixel locations
- M = cv2.moments(cnts[i]) # the contour moments
- center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"])) # x and Y center of mass
- # only proceed if the rectangle meets a minimum size
- # only look in the bottom third part of the image, overlook the car
- if 250 < center[1] < 350:
- print(w,h)
- if 20 < w < 50 and 20 < h < 40:
- # draw the box and centroid on the image
- cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
- #annotate the bounding box to a new file
- up_x = x
- up_y = y
- down_x = x + w
- down_y = y + h
- with open(imagePath +".txt","a+") as dataFile:
- dataFile.write(" ".join([" cone",str(up_x),str(up_y),str(down_x),str(down_y)]))
- #
- # #show the resulted image (and optionally mask and res)
- # cv2.imshow('image', image)
- # # # cv2.imshow('mask',mask)
- # # # cv2.imshow('res',res)
- # # # #0 = wait indefinitely
- # # # #exit when escape key is pressed
- # key = cv2.waitKey(0)
- # if key == 27: # escape
- # break
- # #
- # # # close any open windows
- # cv2.destroyAllWindows()
- #
- # xml_files = []
- # #get the text files
- # def get_txt_files(path):
- # for file in os.listdir(path):
- # extension = os.path.splitext(file)[1]
- # if extension == ".txt":
- # xml_files.append(os.path.join(path, file))
- # else:
- # continue
- #
- # get_txt_files
- # xml_files = sorted(xml_files)
- # #the xml format
- # def create_xml(text,name):
- # xmls = ['<annotation>\n', '\t<folder>VOC2007</folder>\n', '\t<filename>000026.jpg</filename>\n', '\t<source>\n', '\t\t<database>The VOC2007 Database</database>\n', '\t\t<annotation>PASCAL VOC2007</annotation>\n', '\t\t<image>flickr</image>\n', '\t\t<flickrid>192073981</flickrid>\n', '\t</source>\n', '\t<owner>\n', '\t\t<flickrid>tobeng</flickrid>\n', '\t\t<name>kfir_yotam</name>\n', '\t</owner>\n', '\t<size>\n', '\t\t<width>1280</width>\n', '\t\t<height>720</height>\n', '\t\t<depth>3</depth>\n', '\t</size>\n', '\t<segmented>0</segmented>\n', '\t<object>\n', '\t\t<name>car</name>\n', '\t\t<pose>unspecified</pose>\n', '\t\t<truncated>0</truncated>\n', '\t\t<difficult>0</difficult>\n', '\t\t<bndbox>\n', '\t\t\t<xmin>90</xmin>\n', '\t\t\t<ymin>125</ymin>\n', '\t\t\t<xmax>337</xmax>\n', '\t\t\t<ymax>212</ymax>\n', '\t\t</bndbox>\n', '\t</object>\n', '</annotation>\n']
- # annotation_data = text.read()
- # file_name = name
- # object_name = "cone"
- # x_min = annotation_data.split(' ')[2]
- # y_min = annotation_data.split(' ')[3]
- # x_max = annotation_data.split(' ')[4]
- # y_max = annotation_data.split(' ')[5]
- # xmls[2]= '\t<filename>'+file_name+'</filename>\n'
- # xmls[20]= '\t\t<name>'+object_name+'</name>\n'
- # xmls[25]= '\t\t\t<xmin>'+x_min+'</xmin>\n'
- # xmls[26]= '\t\t\t<ymin>'+y_min+'</ymin>\n'
- # xmls[27]= '\t\t\t<xmax>'+x_max+'</xmax>\n'
- # xmls[28]= '\t\t\t<ymax>'+y_max+'</ymax>\n'
- # return xmls
- #
- # #write a list of XML to file
- # def write_to_file(xmls,file_name):
- # f = open(file_name,'w')
- # for line in xmls:
- # f.write(line)
- # f.close()
- #
- # def convert(file_name, text):
- # xmls = create_xml(text,name)
- # write_to_file(xmls,file_name.split('.')[0]+'.xml')
- # #loop through .txt files and create .xml
- # for j in range(len(xml_files)):
- # text = open(xml_files[j],'r')
- # name = os.path.basename(xml_files[j])
- # name = os.path.splitext(name)[0]
- # convert(name, text)
- #
- #
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement