功能:实现将“手动标定”时进行车辆检测标定,区域标定等,并将获得的参数按C结构体结构转成二进制流输出。
语言/工具:python /glade
简单贴吧,不作修正。。。
先看效果吧:
- # -*- coding:utf-8 -*-
- import pygtk, gtk, gtk.glade
- import struct
- #import math
- import sys
- import os
- import pickle
- DrawPoint = 0
- PointList = []
- DrawLinePoints_List = []
- AllAreaPoints_List = []
- AllLinePoints = []
- AllPoints = []
- Dlg = None
- height = 0
- width = 0
- value = ''
- for i in range(254):
- value += struct.pack('@i', 0)
- dic = {'AutoOrManuCoordinate' : value, 'RoadOrArea' : value, 'CarLine' : value}
- try:
- PIC = sys.argv[1]
- #file_name = sys.argv[2]
- file_name = "/home/jack/11517"
- backup_filename = "/home/jack/11518"
- gdXML = gtk.glade.XML("/home/jack/biaoding.glade")
- except:
-
- print "\n************************************* Args error! 1 Args are expected. ****************************************\n"
- #print "\tArg1 should be a picture name with path.\n\tArg2 is a file name with path for saving.\n"
- print "\tUsage like:\n\t\tpython biaoding.py /home/jack/c.jpg\n"
- print "****************************************************************************************************************\n"
- class widgets:
-
- def __init__(self, xml, pic):
- self.picture = gtk.gdk.pixbuf_new_from_file(pic)
-
- self.window = gdXML.get_widget("window1")
- self.window.connect("destroy_event", self.main_quit)
- self.window.connect("delete_event", self.main_quit)
- self.image1 = gdXML.get_widget("image1")
- self.image1.set_from_pixbuf(self.picture)
- #import pdb; pdb.set_trace()
- self.width,self.height= self.image1.get_pixbuf().get_width(), self.image1.get_pixbuf().get_height()
- width, height = self.width, self.height
-
- self.eventbox1 = gdXML.get_widget("eventbox1")
- self.eventbox1.connect("button_release_event", on_eventbox1_button_release_event)
- self.bn1 = gdXML.get_widget("button1")
- self.bn2 = gdXML.get_widget("button2")
- self.bn3 = gdXML.get_widget("button3")
- self.bn4 = gdXML.get_widget("button4")
- self.bn5 = gdXML.get_widget("button5")
- self.bn6 = gdXML.get_widget("button6")
- self.bn7 = gdXML.get_widget("button7")
- self.bn8 = gdXML.get_widget("button8")
- self.bn11 = gdXML.get_widget("button11")
-
- self.bn1.connect("clicked", on_bn1_clicked)
- self.bn2.connect("clicked", load)
- self.bn3.connect("clicked", roadCoordinate)
- self.bn4.connect("clicked", maskLoadLine)
- self.bn5.connect("clicked", maskArea)
- self.bn6.connect("clicked", maskCar)
- self.bn7.connect("clicked", save)
- self.bn8.connect("clicked", self.main_quit)
- self.bn11.connect("clicked", remove)
- self.window.show_all()
-
- def main_quit(self, widget, data= None):
- gtk.main_quit()
- def reflash_image():
-
- picture = gtk.gdk.pixbuf_new_from_file(PIC)
- image1 = gdXML.get_widget("image1")
- image1.set_from_pixbuf(picture)
- image1.show()
- def init_dialog(widget):
-
- dialog1 = gdXML.get_widget("dialog1")
- widget = dialog1
- bn9 = gdXML.get_widget("button9")
- bn10 = gdXML.get_widget("button10")
- bn9.connect("released",on_bn9_released)
- bn10.connect("clicked",on_bn10_clicked)
- dialog1.show()
-
- return dialog1
-
- def on_bn9_released(widget):
- global AreaDirection
-
- dialog1 = gdXML.get_widget("dialog1")
- entry = gdXML.get_widget("entry1")
- AreaDirection.append(entry.get_text())
- dialog1.hide()
- def on_bn10_clicked(widget):
- pass
- def on_eventbox1_button_release_event(widget, event):
-
- global IsPointCalibrate
- global IsRoadLine
- global IsMaskArea
- global IsCarStatisLine
-
- if IsMaskArea == 1 or IsCarStatisLine == 1 or IsRoadLine == 1 or IsPointCalibrate == 1:
- drawing(widget, event)
- def drawing(widget, event):
-
- global DrawPoint
- global PointList
- global DrawLinePoints_List
- global AllAreaPoints_List
- global AreaNum
- global AllLinePoints
- global AreaPointNum_List
- global IsCarStatisLine
- global AreaDirection
- global Dlg
- global CarLineNum
-
- #import pdb;pdb.set_trace()
- if DrawPoint == 1:
- if event.button == 1:
- a_point = {}
- a_point['x'] = event.x
- a_point['y'] = event.y
- PointList.append(a_point)
- DrawLinePoints_List.append(a_point['x'])
- DrawLinePoints_List.append(a_point['y'])
- #标记坐标点
- gdkwin = widget.window
- gc = gdkwin.new_gc()
- gc.foreground = gdkwin.get_colormap().alloc_color("yellow")
- gc.line_width = 2
- gdkwin.draw_arc(gc, False, int(a_point['x']), int(a_point['y']), 2, 2, 0, 360*64)
-
- if IsPointCalibrate == 1 or IsRoadLine == 1:
- AllPoints.append(a_point)
- print AllPoints
- DrawPoint = 2
- if IsCarStatisLine == 1:
- #对车检线若只画一条线,右键结束画线
- if event.button == 3:
- DrawPoint = 0
- CarLineNum = 1
-
-
- elif DrawPoint == 2:
- if event.button == 1:
- b_point = {}
- b_point['x'] = event.x
- b_point['y'] = event.y
- PointList.append(b_point)
- DrawLinePoints_List.append(b_point['x'])
- DrawLinePoints_List.append(b_point['y'])
-
- if IsPointCalibrate == 1 or IsRoadLine == 1:
- AllPoints.append(b_point)
-
- gdkwin = widget.window
- gc = gdkwin.new_gc()
- #标记坐标
- gc.foreground = gdkwin.get_colormap().alloc_color("yellow")
- gc.line_width = 2
- gdkwin.draw_arc(gc, False, int(b_point['x']), int(b_point['y']), 2, 2, 0, 360*64)
-
- gc.foreground = gdkwin.get_colormap().alloc_color("red")
- gc.line_width = 2
- lenth = len(DrawLinePoints_List)
- #if IsMaskArea == 1 or IsCarStatisLine == 1:
- gdkwin.draw_line(gc, DrawLinePoints_List[lenth - 4], DrawLinePoints_List[lenth - 3], DrawLinePoints_List[lenth - 2], DrawLinePoints_List[lenth - 1])
- if IsPointCalibrate == 1:
- if len(AllPoints) >= 4:
- print "4个点.\n"
- DrawPoint = 0
- #else:
- #DrawPoint = 1
- elif IsRoadLine == 1:
- if len(AllPoints) >= 6:
- print "6个点.\n"
- DrawPoint = 0
- else:
- DrawPoint = 1
-
- elif IsCarStatisLine == 1 : #车辆检测线
- AllLinePoints.append(PointList)
- if len(AllLinePoints) < 2:
- DrawPoint = 1
- DrawLinePoints_List = []
- PointList = []
- else:
- DrawPoint = 0
- PointList = []
- CarLineNum = 2
-
- if event.button == 3:
- if IsMaskArea == 1:
- gdkwin = widget.window
- gc = gdkwin.new_gc()
- gc.foreground = gdkwin.get_colormap().alloc_color("red")
- gc.line_width = 2
- lenth = len(DrawLinePoints_List)
- gdkwin.draw_line(gc, DrawLinePoints_List[lenth - 2], DrawLinePoints_List[lenth - 1], DrawLinePoints_List[0], DrawLinePoints_List[1])
- if Dlg == None:
- Dlg = init_dialog(widget)
- else:
- Dlg.show()
- AllAreaPoints_List.append(PointList)
- AreaNum = len(AllAreaPoints_List)
- if AreaNum < 10:
- DrawLinePoints_List = []
- PointList = []
- DrawPoint = 1
- else:
- DrawPoint = 0
- print "error:AreaNum > 10.\n"
-
- def on_bn1_clicked(widget):
-
- global IsPointCalibrate
- global IsRoadLine
- global IsMaskArea
- global IsCarStatisLine
-
- print "自动标定 clicked.\n"
- IsPointCalibrate = 0
- IsRoadLine = 0
- IsMaskArea = 0
- IsCarStatisLine = 0
- init_para()
- reflash_image()
- print IsPointCalibrate
-
- def roadCoordinate(widget):
-
- global IsMaskArea
- global DrawPoint
- global IsCarStatisLine
- global CarLineNum
- global IsPointCalibrate
- global IsRoadLine
-
- IsPointCalibrate = 1
- IsMaskArea = 0
- IsRoadLine = 0
- IsCarStatisLine = 0
- CarLineNum = 0
-
- print "路面坐标标定.\n"
- init_para()
- reflash_image()
- DrawPoint = 1
- def maskLoadLine(widget):
-
- global IsRoadLine
- global IsMaskArea
- global DrawPoint
- global IsCarStatisLine
- global CarLineNum
- global IsPointCalibrate
-
- IsRoadLine = 1
- IsMaskArea = 0
- IsCarStatisLine = 0
- CarLineNum = 0
- IsPointCalibrate = 0
-
- print "车道标定.\n"
- init_para()
- reflash_image()
- DrawPoint = 1
- def maskArea(widget):
-
- global IsRoadLine
- global IsMaskArea
- global DrawPoint
- global IsCarStatisLine
- global IsPointCalibrate
- global CarLineNum
-
- print "区域标定.\n"
- IsRoadLine = 0
- IsMaskArea = 1
- IsCarStatisLine = 0
- IsPointCalibrate = 0
- CarLineNum = 0
- init_para()
- reflash_image()
- DrawPoint = 1
-
- def maskCar(widget):
-
- global IsCarStatisLine
- global IsRoadLine
- global IsMaskArea
- global IsPointCalibrate
- global DrawPoint
- global CarLineNum
-
- print "车辆检测线标定.\n"
-
- IsCarStatisLine = 1
- IsRoadLine = 0
- IsMaskArea = 0
- CarLineNum = 0
- IsPointCalibrate = 0
-
- init_para()
- reflash_image()
- DrawPoint = 1
-
- #坐标排序
- def coordinate_scheduling(points):
-
- global IsCarStatisLine
- global height
- global width
- #xy = []
- XY = []
-
- #if IsCarStatisLine == 1:
-
- #for i in range(len(points)):
- #XY.append((int(points[i]['x']),int(points[i]['y'])))
- #print XY
- #XY.sort(key = lambda x:x[1]) # 保证X相等时按Y的顺序排序
- #XY.sort(key = lambda x:x[0])
- #print XY
- #else:
- #for i in range(len(points)):
- #s = math.sqrt((width - int(points[i]['x'])) * (width - int(points[i]['x'])) + (height - int(points[i]['y'])) * (height - int(points[i]['y'])))
- #xy.append((int(points[i]['x']), int(points[i]['y']), s))
- #xy.sort(key = lambda x:-x[1])
- #print xy
- #xy.sort(key = lambda x:x[2])
- #print xy
- #for i in range(len(xy)):
- #X, Y, Z = xy[i]
- #XY.append((X, Y))
- #print XY
- for i in range(len(points)):
- XY.append((int(points[i]['x']),int(points[i]['y'])))
- if IsCarStatisLine == 1:
- XY.sort(key = lambda x:x[1]) # 保证X相等时按Y的顺序排序
- XY.sort(key = lambda x:x[0])
- print XY
- else:
- XY.sort(key = lambda x:-x[0])
- XY.sort(key = lambda x:-x[1])
-
-
- return XY
-
- def save_data():
-
- global AreaPointNum_List
- global AllAreaPoints_List
- global AreaNum
- global AreaDirection
- global AllLinePoints
- global AllPoints
- global CarLineNum
-
- global IsPointCalibrate
- global IsRoadLine
- global IsMaskArea
- global IsCarStatisLine
-
- global dic
- global value
- MAreaDirection = []
- MAreaPointNum = []
- L_Points = []
- BinData = ''
-
- data1 = struct.pack('@5i',IsPointCalibrate, IsRoadLine, IsMaskArea, IsCarStatisLine, AreaNum)
- data2, data3, data4, data5, data6 = '', '', '', '', ''
-
- print IsPointCalibrate, IsRoadLine, IsMaskArea, IsCarStatisLine
- #if IsPointCalibrate == 1:
- if IsMaskArea == 1:
- for i in range(len(AllAreaPoints_List)):
- length = len(AllAreaPoints_List[i])
- AreaPointNum_List.append(length)
- print AreaPointNum_List
-
- for i in range(len(AreaPointNum_List)):
- MAreaPointNum.append(int(AreaPointNum_List[i]))
- MAreaDirection.append(int(AreaDirection[i]))
- print MAreaDirection,MAreaPointNum, IsPointCalibrate, IsRoadLine, IsCarStatisLine, IsMaskArea, AreaNum, AllAreaPoints_List
-
- point_sum = 0
- for i in range(AreaNum):
- data2 += struct.pack('@i', MAreaPointNum[i])
- data3 += struct.pack('@i', MAreaDirection[i])
- point_sum += MAreaPointNum[i]
- for j in range(len(AllAreaPoints_List[i])):
- data4 += struct.pack('@2i', AllAreaPoints_List[i][j]['x'], AllAreaPoints_List[i][j]['y'])
- print point_sum
-
- for i in range(21):
- data5 += struct.pack('@i', 0)
- for i in range(4):
- data6 += struct.pack('@2i', 0, 0)
-
- data2_in, data3_in, data4_in, data5_in = '', '', '', ''
- if AreaNum < 10:
- for i in range(10-AreaNum):
- data2_in += struct.pack('@i', 0)
- data3_in += struct.pack('@i', 0)
- for i in range(100 - point_sum):
- data4_in += struct.pack('@2i', 0, 0)
- BinData = data1 + data2 + data2_in + data3 + data3_in + data5 + data4 + data4_in + data6
- else:
- BinData = data1 + data2 + data3 + data5 + data4 + data6
-
- dic['RoadOrArea'] = BinData
- print "lb = %d\n"%(len(dic['RoadOrArea']))
-
- elif IsRoadLine == 1 or IsPointCalibrate == 1 or IsCarStatisLine == 1:
- print AllPoints
- L_Points = coordinate_scheduling(AllPoints)
- print L_Points
- for i in range(21):
- data2 += struct.pack('@i',0)
-
- if IsPointCalibrate == 1:
- for i in range(len(L_Points)):
- x,y = L_Points[i]
- data3 += struct.pack('@2i',x,y )
- for i in range(220):
- data4 += struct.pack('@i',0)
- BinData = data1 + data2 + data3 + data4
-
- dic['AutoOrManuCoordinate'] = BinData
- print "la = %d\n"%(len(dic['AutoOrManuCoordinate']))
-
-
- elif IsRoadLine == 1:
- for i in range(4):
- data3 += struct.pack('@2i', 0, 0)
- for i in range(len(L_Points)):
- x,y = L_Points[i]
- data4 += struct.pack('@2i', x, y)
- for i in range(104):
- data5 += struct.pack('@2i', 0 ,0)
- BinData = data1 + data2 + data3 + data4 + data5
-
- dic['RoadOrArea'] = BinData
- #print "lb = %d\n"%(len(dic['RoadOrArea']))
- elif IsCarStatisLine == 1:
- data2 = ''
- for i in range(20):
- data2 += struct.pack('@i', 0)
- data3 = struct.pack('@i', CarLineNum)
- for i in range(110):
- data4 += struct.pack('@2i', 0, 0)
- a_Line, xy = [], []
- print len(AllLinePoints)
- for i in range(len(AllLinePoints)):
- a_Line.append(AllLinePoints[i])
- for i in range(len(a_Line)):
- xy = coordinate_scheduling(a_Line[i])
- for i in range(len(xy)):
- x,y = xy[i]
- data5 += struct.pack('@2i', x, y)
- if len(AllLinePoints) != 2:
- data5_in = struct.pack('@4i', 0, 0, 0 ,0)
- BinData = data1 + data2 + data3 + data4 + data5 + data5_in
- else:
- BinData = data1 + data2 + data3 + data4 + data5
-
- dic['CarLine'] = BinData
- print "lc = %d\n"%(len(dic['CarLine']))
-
- elif IsPointCalibrate == 0:
- for i in range(254):
- BinData += struct.pack('@i',0)
- dic['AutoOrManuCoordinate'] = BinData
- print "la = %d\n"%(len(dic['AutoOrManuCoordinate']))
-
- print dic
- a, b, c, d = [], [], [], []
- all_Bin = ''
- for i in range(254):
- a.append(struct.unpack('@254i', dic['AutoOrManuCoordinate'])[i])
- b.append(struct.unpack('@254i', dic['RoadOrArea'])[i])
- c.append(struct.unpack('@254i', dic['CarLine'])[i])
- d.append(a[i]^b[i]^c[i])
- all_Bin += struct.pack('@i',d[i])
- print d
-
-
-
- #backup
- f = open(backup_filename, 'wb')
- pickle.dump(all_Bin, f)
- f.close()
-
- write_to_file(all_Bin)
-
- #write to file
- def write_to_file(a):
- f = open(file_name, 'wb')
- f.write(a)
- f.close()
-
- def load(widget):
- f = open(backup_filename, 'rb')
- a = pickle.load(f)
- f.close()
-
- write_to_file(a)
-
- def remove(widget):
- global dic
- global IsPointCalibrate
- global IsRoadLine
- global IsMaskArea
- global IsCarStatisLine
-
- IsPointCalibrate = 0
- IsRoadLine = 0
- IsMaskArea = 0
- IsCarStatisLine = 0
-
- cmd = 'cat /dev/null > %s'%(file_name)
- os.system(cmd)
- dic = {'AutoOrManuCoordinate' : value, 'RoadOrArea' : value, 'CarLine' : value,}
-
-
- def save(widget):
- print "save button clicked.\n"
-
- global IsPointCalibrate
- global IsRoadLine
- global IsMaskArea
- global IsCarStatisLine
-
- save_data()
- init_para()
- reflash_image()
- IsPointCalibrate = 0
- IsRoadLine = 0
- IsMaskArea = 0
- IsCarStatisLine = 0
-
- def init_para():
-
- global DrawPoint
- global PointList
- global DrawLinePoints_List
- global AllAreaPoints_List
- global AllLinePoints
- global AreaNum
- global AllPoints
- global AreaPointNum_List
- DrawPoint = 0
- PointList = []
- DrawLinePoints_List = []
- AllAreaPoints_List = []
- AllLinePoints = []
- AllPoints = []
- AreaNum = 0
- AreaPointNum_List = []
-
- if __name__ == '__main__':
-
- IsPointCalibrate = 0
- IsRoadLine = 0
- IsMaskArea = 0
- IsCarStatisLine = 0
- AreaNum = 0
- AreaPointNum_List = []
- AreaDirection = []
- CarLineNum = 0
- win = widgets(gdXML, PIC)
- gtk.main()
阅读(2290) | 评论(0) | 转发(0) |