Pytthon Learning Scripts

Source: Internet
Author: User
Tags readline
convert_fence_label.py Import OS import cv2 import resource_pb2 import hadmap_pb2 import Google import google.protobuf Imp ORT Google.protobuf.text_format Import Threading Import NumPy as NP import Argparse FLAGS = None def convert (Label_dir, I Mage_dir, Out_dir, Lane_num, Crop_h, Resize_w, Resize_h, Debug): data = None Line_num = 0 filenames = os.listd
        IR (LABEL_DIR) for filename in filenames:if os.path.splitext (filename) [-1]!= '. Road ': Continue filepath = Os.path.join (label_dir, filename) proto_file = open (filepath, ' r ') Proto_str = Proto_file . read () Pbout = Resource_pb2. Uplaodlaneline2drequest () Google.protobuf.text_format. Merge (Proto_str, pbout) image_name = filename[0:-5] + '. jpg ' image = Cv2.imread (Os.path.join (Image_dir, IM Age_name)) If image is None:print ' can not ' open image ' continue height = image.sh Ape[0] width = image.shape[1] #Print ("h=%d,w=%d"% (height, width)) step = float (crop_h)/float (lane_num) resize_ratio_w = float (resize_
        W)/Width Resize_ratio_h = float (resize_h)/Height image = cv2.resize (image, (Resize_w, Resize_h)) h = image.shape[0] W = image.shape[1] #print len (pbout.lane_line) print (image_name) ha S_fence = False with open (Os.path.join (Out_dir, image_name.replace (' jpg ', ' txt ')), ' W ') as Outfile:la Ne_group = {} for Lane in Pbout.lane_line:if Lane.type!= hadmap_pb2.
                Line_type_fence:continue has_fence = True tab = Int (lane.lane_id)
                Print tab if tab not in Lane_group:lane_group[tab] = {"X": [], "Y": []} lane_group[tab][' x '] + = (Np.array ([(p.x + WIDTH/2) for P in Lane.center_line]) * resize_ratio_w). Astype (NP . Int32). ToList () Lane_group[tab][' y '] + = (Np.array ([(-p.y + HEIGHT/2) for P in Lane.center_line]) * resize_ratio_h). Astype (Np.int32). ToList () #print (Lane_group) #print ([x for X-lane_group]) #print ([lane_group[x][' type '] for x in Lane_g Roup]) If debug:for TBS in Lane_group:for p in range (len (lane_group[tbs][' x ')

        ): Cv2.circle (Image, (lane_group[tbs][' x '][p], lane_group[tbs][' y '][p]), 2, (255, 0, 0), 2) 
            If Debug:print (image_name) if Has_fence:cv2.imwrite (' debug/' + image_name, image)
            #cv2. Imshow ("Press ESC to quit", image) #k = Cv2.waitkey (0) #if k = = 27: # break If __name__ = = ' __main__ ': parser = argparse.
        Argumentparser () parser.add_argument (' d ', '--imagedir ', type=str, default= ' checked_images/', Help= ' The images dir. ') parser.add_argument (' I ', '--labeldir', Type=str, default= ' checked_labels/', help= ' the Images dir. ') parser.add_argument (  ' O ', '--outdir ', Type=str, Required=false, default= './labels ', help= ' the output labels  Dir. ') parser.add_argument (' l ', '--lanenum ', Type=int, default=11, # 64 = 352 * 2/11, downsample=2, Input_h = 352, Feature_map_h = help= ' The step size of Lane gap, pixels in raw image, default=57.  1=314 * 2/11 ') parser.add_argument ('-C ', '--crop ', Type=int, default=352, # 704 = 352 *
        2 help= ' croped height of the image, Default=628=314*2 ') parser.add_argument (' Y ', '--resize_h ',
        Type=int, default=604, help= ' height of the ' image after resize ') parser.add_argument ( '-X ', '--resize_w ', Type=int, default=960, help= ' width of the ' image after resize ') par
      Ser.add_argument (  '--debug ', action= ' store_true ', Default=false) FLAGS, unparsed = Parser.parse_known_args () Print (FLAGS) if not os.path.exists (Flags.outdir): Os.mkdir (flags.outdir) convert (Flags.labeldir, flags.im Agedir, Flags.outdir, Flags.lanenum, Flags.crop, Flags.resize_w, Flags.resize_h, Flags.debug) os.system (' Find ' + FLAG S.outdir + '-size 0 | Xargs-r-I rm ' {} ' ' #delete The Empty label os.system (' ls ' + flags.outdir + ' | sed s/txt/jpg/g >list.txt ') #gen Erate the Data list import os Label_dir = ' labels_fence ' output_dir = ' output ' files = Os.listdir (Label_dir) for I, file
    Name in enumerate (files): Print ("%d/%d:%s"% (i, Len (files), filename) path = Os.path.join (label_dir, filename)
            OK = True with the Open (path) as F:points = [] While true:line = F.readline (). Strip () if line = = ': Break If Len (Line.split ('))!= 4:os.system (' echo%s ≫>error.txt '% filename] ok = False break x1,y1,x2,y2 = Line.split (') Points.append ([Float (x1), float (y1), float (x2), float (y2)]) if not ok:continue Out_pa
                th = Os.path.join (output_dir, filename) with open (Out_path, ' W ") as O:for p in points:
                t = 4 if (p[0] = = 0 and p[1] = = 0) or (p[2] = 0 and p[3] ==0): t =-1  O.write (' 3%d%f%f%f\n '% (t, (p[0) + p[2])/2, p[1], p[2]-p[0]) #for p in points: # t  = 4 # if p[0] = = 0 and p[1] = = 0: # t =-1 # o.write (' 4%f%f%f '%

(t, P[2], p[3], 0)) import os label_dir = ' labels ' output_dir = ' labels_new ' files = Os.listdir (Label_dir) for I, filename in enumerate (files) : Print ("%d/%d:%s"% (i, Len (files), filename) path = Os.path.join (label_dir, filename) ok = True with ope NPath) as F:points = [] lanes = {} Has_left = False while true:line = F.readl INE (). Strip () if line = = ': Break If Len (Line.split ('))!= 5:o S.system (' echo%s >>error.txt '% filename) ok = False Break Lane = line.
                Split (') group, T, X, y, w = Lane if Has_left = = False and group = ' 1 ' and T!= '-1 ':
                Has_left = True If group not in lanes:lanes[group] = [Lane] Else: Lanes[group].append (Lane) if not ok:continue Out_path = Os.path.join (output_dir, file
                    Name) with open (Out_path, "w") as O:for l in [' 1 ', ' 0 ', ' 2 ']: for P in Lanes[l]: G = 0 if has_left:if l = = ' 0 ': g =
                      1  elif L = = ' 1 ': g = 0 Else:g = 2 Else:if L = = ' 0 ': g = 0 elif L =
                    = ' 1 ': g = 0 Else:g = 1
            Group, T, X, y, w = P o.write ('%d%s%s%s\n '% (g,t,x,y,w)) #for p in points: # t = 4 # if p[0] = = 0 and p[1] = = 0: # t =-1 # o.write (' 4%f %f%f%f\n '% (t, p[2, P[3], 0)) Import os import math Label_dir = ' labels_only_bollard_center ' output_dir = ' output ' fi  Les = Os.listdir (Label_dir) for I, filename in enumerate (files): Print ("%d/%d:%s"% (i, Len (files), filename) path
            = Os.path.join (label_dir, filename) ok = True with the Open (path) as F:points = [] while True: line = F.readline(). Strip () if line = = ': Break If Len (Line.split ('))!= 4:OS.S Ystem (' echo%s >>error.txt '% filename) ok = False break g, T, Xs,ys,ws  = Line.split (') x = float (xs) y = float (ys) w = float (ws) if T!= 0 and X==0 and Y ==0:print Line exit () if x > 3 or y > 3 or X <-3 or Y & Lt -3 or W > 5 or W < 0:print line exit ()
#! /usr/bin python #-*-coding:utf-8-*-import cv2 Import copy as CP import OS infilename= "/home/zhao/documents/20180117_r Ain_chery_4.h2640658.jpg "image = Cv2.imread (infilename, 1) img_row_start = 768#192#628#560 448#560#530 img_height_in

= 768#768#718#640#640 Resize_height = 1536#960#1346#996#1200 resize_width = 1536#1920 ' ' 628 560 718 640 1346 1536 ' "' 718 640 718 640 1436 1536 '" ' 402 360 718 640 1120 1536 ' "' Image_cut = cv2.resize (image, resize _width, resize_height)) Image_resize = Image_cut[img_row_start:img_row_start + img_height_in,:,:] Cv2.namedWindow (' Before ', Cv2.
Window_normal) Cv2.resizewindow ("Before", IMAGE_RESIZE.SHAPE[1]/2, IMAGE_RESIZE.SHAPE[0]/2);


Print "before shape:" + str (image_resize.shape[0]) + "" + str (image_resize.shape[1]) cv2.imshow (' before ', image_resize) Infilename= "/home/zhao/documents/43465069059273.jpg" image = Cv2.imread (infilename, 1) Img_row_start = 432#168#424# 168#424 img_height_in = 768#768#768#768 936 reSize_height = 1200#1284 Resize_width = 1536 Image_cut = cv2.resize (image, (Resize_width, resize_height)) Image_resize = i Mage_cut[img_row_start:img_row_start + img_height_in,:,:] Cv2.namedwindow (' Now ', Cv2.
Window_normal) Cv2.resizewindow ("Now", IMAGE_RESIZE.SHAPE[1]/2, IMAGE_RESIZE.SHAPE[0]/2); Print "Now shape:" + str (image_resize.shape[0]) + "" + str (image_resize.shape[1]) cv2.imshow (' Now ', image_resize) ' I Nfilename= "/home/zhao/documents/43465069059273.jpg" image = Cv2.imread (infilename, 1) img_row_start = 424#168#424 Img_ height_in = 511#768 936 resize_height = 1285 Resize_width = 1536 Image_cut = cv2.resize (image, Resize_width, Resize_hei ght)) Image_resize = Image_cut[img_row_start:img_row_start + img_height_in,:,:] image_cut = cv2.resize (image, Resize_w Idth, 768)) Cv2.namedwindow (' Now_ ', Cv2.
Window_normal) Cv2.resizewindow ("Now_", IMAGE_RESIZE.SHAPE[1]/2, IMAGE_RESIZE.SHAPE[0]/2); Print "Now_ shape:" + str (image_resize.shape[0]) + "" + str (image_resize.shape[1]) CV2.imshow (' Now_ ', image_resize) ' Cv2.waitkey (0)
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.