Chinaunix首页 | 论坛 | 博客
  • 博客访问: 245662
  • 博文数量: 68
  • 博客积分: 2802
  • 博客等级: 少校
  • 技术积分: 614
  • 用 户 组: 普通用户
  • 注册时间: 2010-01-11 15:38
文章存档

2013年(3)

2012年(15)

2011年(21)

2010年(29)

我的朋友

分类: LINUX

2012-03-09 14:44:31

功能:实现将“手动标定”时进行车辆检测标定,区域标定等,并将获得的参数按C结构体结构转成二进制流输出。
语言/工具:python /glade

简单贴吧,不作修正。。。
先看效果吧:



点击(此处)折叠或打开

  1. # -*- coding:utf-8 -*-
  2. import pygtk, gtk, gtk.glade
  3. import struct
  4. #import math
  5. import sys
  6. import os
  7. import pickle

  8. DrawPoint = 0
  9. PointList = []
  10. DrawLinePoints_List = []
  11. AllAreaPoints_List = []
  12. AllLinePoints = []
  13. AllPoints = []
  14. Dlg = None
  15. height = 0
  16. width = 0

  17. value = ''
  18. for i in range(254):
  19.   value += struct.pack('@i', 0)
  20. dic = {'AutoOrManuCoordinate' : value, 'RoadOrArea' : value, 'CarLine' : value}


  21. try:
  22.   PIC = sys.argv[1]
  23.   #file_name = sys.argv[2]
  24.   file_name = "/home/jack/11517"
  25.   backup_filename = "/home/jack/11518"
  26.   gdXML = gtk.glade.XML("/home/jack/biaoding.glade")
  27. except:
  28.   
  29.   print "\n************************************* Args error! 1 Args are expected. ****************************************\n"
  30.   #print "\tArg1 should be a picture name with path.\n\tArg2 is a file name with path for saving.\n"
  31.   print "\tUsage like:\n\t\tpython biaoding.py /home/jack/c.jpg\n"
  32.   print "****************************************************************************************************************\n"



  33. class widgets:
  34.   
  35.   def __init__(self, xml, pic):

  36.     self.picture = gtk.gdk.pixbuf_new_from_file(pic)
  37.     
  38.     self.window = gdXML.get_widget("window1")
  39.     self.window.connect("destroy_event", self.main_quit)
  40.     self.window.connect("delete_event", self.main_quit)
  41.     self.image1 = gdXML.get_widget("image1")
  42.     self.image1.set_from_pixbuf(self.picture)
  43.     #import pdb; pdb.set_trace()
  44.     self.width,self.height= self.image1.get_pixbuf().get_width(), self.image1.get_pixbuf().get_height()
  45.     width, height = self.width, self.height
  46.     
  47.     self.eventbox1 = gdXML.get_widget("eventbox1")
  48.     self.eventbox1.connect("button_release_event", on_eventbox1_button_release_event)
  49.     self.bn1 = gdXML.get_widget("button1")
  50.     self.bn2 = gdXML.get_widget("button2")
  51.     self.bn3 = gdXML.get_widget("button3")
  52.     self.bn4 = gdXML.get_widget("button4")
  53.     self.bn5 = gdXML.get_widget("button5")
  54.     self.bn6 = gdXML.get_widget("button6")
  55.     self.bn7 = gdXML.get_widget("button7")
  56.     self.bn8 = gdXML.get_widget("button8")
  57.     self.bn11 = gdXML.get_widget("button11")
  58.    
  59.     self.bn1.connect("clicked", on_bn1_clicked)
  60.     self.bn2.connect("clicked", load)
  61.     self.bn3.connect("clicked", roadCoordinate)
  62.     self.bn4.connect("clicked", maskLoadLine)
  63.     self.bn5.connect("clicked", maskArea)
  64.     self.bn6.connect("clicked", maskCar)
  65.     self.bn7.connect("clicked", save)
  66.     self.bn8.connect("clicked", self.main_quit)
  67.     self.bn11.connect("clicked", remove)
  68.     self.window.show_all()
  69.     
  70.   def main_quit(self, widget, data= None):
  71.     gtk.main_quit()

  72. def reflash_image():
  73.   
  74.   picture = gtk.gdk.pixbuf_new_from_file(PIC)
  75.   image1 = gdXML.get_widget("image1")
  76.   image1.set_from_pixbuf(picture)
  77.   image1.show()

  78. def init_dialog(widget):
  79.     
  80.   dialog1 = gdXML.get_widget("dialog1")
  81.   widget = dialog1

  82.   bn9 = gdXML.get_widget("button9")
  83.   bn10 = gdXML.get_widget("button10")
  84.   bn9.connect("released",on_bn9_released)
  85.   bn10.connect("clicked",on_bn10_clicked)
  86.   dialog1.show()
  87.   
  88.   return dialog1
  89.   
  90. def on_bn9_released(widget):

  91.   global AreaDirection
  92.   
  93.   dialog1 = gdXML.get_widget("dialog1")
  94.   entry = gdXML.get_widget("entry1")
  95.   AreaDirection.append(entry.get_text())
  96.   dialog1.hide()

  97. def on_bn10_clicked(widget):
  98.   pass

  99. def on_eventbox1_button_release_event(widget, event):
  100.   
  101.   global IsPointCalibrate
  102.   global IsRoadLine
  103.   global IsMaskArea
  104.   global IsCarStatisLine
  105.   
  106.   if IsMaskArea == 1 or IsCarStatisLine == 1 or IsRoadLine == 1 or IsPointCalibrate == 1:
  107.     drawing(widget, event)

  108. def drawing(widget, event):
  109.   
  110.   global DrawPoint
  111.   global PointList
  112.   global DrawLinePoints_List
  113.   global AllAreaPoints_List
  114.   global AreaNum
  115.   global AllLinePoints
  116.   global AreaPointNum_List
  117.   global IsCarStatisLine
  118.   global AreaDirection
  119.   global Dlg
  120.   global CarLineNum
  121.   
  122.   #import pdb;pdb.set_trace()
  123.   if DrawPoint == 1:
  124.     if event.button == 1:
  125.       a_point = {}
  126.       a_point['x'] = event.x
  127.       a_point['y'] = event.y
  128.       PointList.append(a_point)
  129.       DrawLinePoints_List.append(a_point['x'])
  130.       DrawLinePoints_List.append(a_point['y'])
  131.       #标记坐标点
  132.       gdkwin = widget.window
  133.       gc = gdkwin.new_gc()
  134.       gc.foreground = gdkwin.get_colormap().alloc_color("yellow")
  135.       gc.line_width = 2
  136.       gdkwin.draw_arc(gc, False, int(a_point['x']), int(a_point['y']), 2, 2, 0, 360*64)
  137.       
  138.       if IsPointCalibrate == 1 or IsRoadLine == 1:
  139.         AllPoints.append(a_point)
  140.       print AllPoints
  141.       DrawPoint = 2

  142.     if IsCarStatisLine == 1:
  143.       #对车检线若只画一条线,右键结束画线
  144.       if event.button == 3:
  145.         DrawPoint = 0
  146.         CarLineNum = 1
  147.       
  148.         
  149.   elif DrawPoint == 2:
  150.     if event.button == 1:
  151.       b_point = {}
  152.       b_point['x'] = event.x
  153.       b_point['y'] = event.y
  154.       PointList.append(b_point)
  155.       DrawLinePoints_List.append(b_point['x'])
  156.       DrawLinePoints_List.append(b_point['y'])
  157.       
  158.       if IsPointCalibrate == 1 or IsRoadLine == 1:
  159.         AllPoints.append(b_point)
  160.         
  161.       gdkwin = widget.window
  162.       gc = gdkwin.new_gc()
  163.       #标记坐标
  164.       gc.foreground = gdkwin.get_colormap().alloc_color("yellow")
  165.       gc.line_width = 2
  166.       gdkwin.draw_arc(gc, False, int(b_point['x']), int(b_point['y']), 2, 2, 0, 360*64)
  167.       
  168.       gc.foreground = gdkwin.get_colormap().alloc_color("red")
  169.       gc.line_width = 2
  170.       lenth = len(DrawLinePoints_List)
  171.       #if IsMaskArea == 1 or IsCarStatisLine == 1:
  172.       gdkwin.draw_line(gc, DrawLinePoints_List[lenth - 4], DrawLinePoints_List[lenth - 3], DrawLinePoints_List[lenth - 2], DrawLinePoints_List[lenth - 1])

  173.       if IsPointCalibrate == 1:
  174.         if len(AllPoints) >= 4:
  175.           print "4个点.\n"
  176.           DrawPoint = 0
  177.         #else:
  178.           #DrawPoint = 1
  179.       elif IsRoadLine == 1:
  180.         if len(AllPoints) >= 6:
  181.           print "6个点.\n"
  182.           DrawPoint = 0
  183.         else:
  184.           DrawPoint = 1
  185.         
  186.       elif IsCarStatisLine == 1 : #车辆检测线
  187.         AllLinePoints.append(PointList)

  188.         if len(AllLinePoints) < 2:
  189.           DrawPoint = 1
  190.           DrawLinePoints_List = []
  191.           PointList = []
  192.         else:
  193.           DrawPoint = 0
  194.           PointList = []
  195.           CarLineNum = 2
  196.           
  197.     if event.button == 3:
  198.       if IsMaskArea == 1:
  199.         gdkwin = widget.window
  200.         gc = gdkwin.new_gc()
  201.         gc.foreground = gdkwin.get_colormap().alloc_color("red")
  202.         gc.line_width = 2
  203.         lenth = len(DrawLinePoints_List)
  204.         gdkwin.draw_line(gc, DrawLinePoints_List[lenth - 2], DrawLinePoints_List[lenth - 1], DrawLinePoints_List[0], DrawLinePoints_List[1])
  205.         if Dlg == None:
  206.           Dlg = init_dialog(widget)
  207.         else:
  208.           Dlg.show()
  209.         AllAreaPoints_List.append(PointList)
  210.         AreaNum = len(AllAreaPoints_List)

  211.         if AreaNum < 10:
  212.           DrawLinePoints_List = []
  213.           PointList = []
  214.           DrawPoint = 1
  215.         else:
  216.           DrawPoint = 0
  217.           print "error:AreaNum > 10.\n"
  218.         
  219. def on_bn1_clicked(widget):
  220.   
  221.   global IsPointCalibrate
  222.   global IsRoadLine
  223.   global IsMaskArea
  224.   global IsCarStatisLine
  225.   
  226.   print "自动标定 clicked.\n"
  227.   IsPointCalibrate = 0
  228.   IsRoadLine = 0
  229.   IsMaskArea = 0
  230.   IsCarStatisLine = 0
  231.   init_para()
  232.   reflash_image()
  233.   print IsPointCalibrate
  234.   
  235. def roadCoordinate(widget):
  236.   
  237.   global IsMaskArea
  238.   global DrawPoint
  239.   global IsCarStatisLine
  240.   global CarLineNum
  241.   global IsPointCalibrate
  242.   global IsRoadLine
  243.   
  244.   IsPointCalibrate = 1
  245.   IsMaskArea = 0
  246.   IsRoadLine = 0
  247.   IsCarStatisLine = 0
  248.   CarLineNum = 0
  249.   
  250.   print "路面坐标标定.\n"
  251.   init_para()
  252.   reflash_image()
  253.   DrawPoint = 1

  254. def maskLoadLine(widget):
  255.   
  256.   global IsRoadLine
  257.   global IsMaskArea
  258.   global DrawPoint
  259.   global IsCarStatisLine
  260.   global CarLineNum
  261.   global IsPointCalibrate
  262.   
  263.   IsRoadLine = 1
  264.   IsMaskArea = 0
  265.   IsCarStatisLine = 0
  266.   CarLineNum = 0
  267.   IsPointCalibrate = 0
  268.   
  269.   print "车道标定.\n"
  270.   init_para()
  271.   reflash_image()
  272.   DrawPoint = 1

  273. def maskArea(widget):
  274.   
  275.   global IsRoadLine
  276.   global IsMaskArea
  277.   global DrawPoint
  278.   global IsCarStatisLine
  279.   global IsPointCalibrate
  280.   global CarLineNum
  281.   
  282.   print "区域标定.\n"
  283.   IsRoadLine = 0
  284.   IsMaskArea = 1
  285.   IsCarStatisLine = 0
  286.   IsPointCalibrate = 0
  287.   CarLineNum = 0

  288.   init_para()
  289.   reflash_image()
  290.   DrawPoint = 1
  291.   

  292. def maskCar(widget):
  293.   
  294.   global IsCarStatisLine
  295.   global IsRoadLine
  296.   global IsMaskArea
  297.   global IsPointCalibrate
  298.   global DrawPoint
  299.   global CarLineNum
  300.   
  301.   print "车辆检测线标定.\n"
  302.   
  303.   IsCarStatisLine = 1
  304.   IsRoadLine = 0
  305.   IsMaskArea = 0
  306.   CarLineNum = 0
  307.   IsPointCalibrate = 0
  308.   
  309.   init_para()
  310.   reflash_image()
  311.   DrawPoint = 1
  312.   
  313. #坐标排序
  314. def coordinate_scheduling(points):
  315.   
  316.   global IsCarStatisLine
  317.   global height
  318.   global width
  319.   #xy = []
  320.   XY = []
  321.   
  322.   #if IsCarStatisLine == 1:
  323.     
  324.     #for i in range(len(points)):
  325.       #XY.append((int(points[i]['x']),int(points[i]['y'])))
  326.     #print XY
  327.     #XY.sort(key = lambda x:x[1]) # 保证X相等时按Y的顺序排序
  328.     #XY.sort(key = lambda x:x[0])
  329.     #print XY
  330.   #else:
  331.     #for i in range(len(points)):
  332.       #s = math.sqrt((width - int(points[i]['x'])) * (width - int(points[i]['x'])) + (height - int(points[i]['y'])) * (height - int(points[i]['y'])))
  333.       #xy.append((int(points[i]['x']), int(points[i]['y']), s))
  334.     #xy.sort(key = lambda x:-x[1])
  335.     #print xy
  336.     #xy.sort(key = lambda x:x[2])
  337.     #print xy
  338.     #for i in range(len(xy)):
  339.       #X, Y, Z = xy[i]
  340.       #XY.append((X, Y))
  341.     #print XY
  342.   for i in range(len(points)):
  343.     XY.append((int(points[i]['x']),int(points[i]['y'])))
  344.   if IsCarStatisLine == 1:
  345.     XY.sort(key = lambda x:x[1]) # 保证X相等时按Y的顺序排序
  346.     XY.sort(key = lambda x:x[0])
  347.     print XY
  348.   else:
  349.     XY.sort(key = lambda x:-x[0])
  350.     XY.sort(key = lambda x:-x[1])
  351.     
  352.     
  353.   return XY
  354.    
  355. def save_data():
  356.   
  357.   global AreaPointNum_List
  358.   global AllAreaPoints_List
  359.   global AreaNum
  360.   global AreaDirection
  361.   global AllLinePoints
  362.   global AllPoints
  363.   global CarLineNum
  364.   
  365.   global IsPointCalibrate
  366.   global IsRoadLine
  367.   global IsMaskArea
  368.   global IsCarStatisLine
  369.   
  370.   global dic
  371.   global value

  372.   MAreaDirection = []
  373.   MAreaPointNum = []
  374.   L_Points = []
  375.   BinData = ''
  376.   
  377.   data1 = struct.pack('@5i',IsPointCalibrate, IsRoadLine, IsMaskArea, IsCarStatisLine, AreaNum)
  378.   data2, data3, data4, data5, data6 = '', '', '', '', ''
  379.   
  380.   print IsPointCalibrate, IsRoadLine, IsMaskArea, IsCarStatisLine
  381.   #if IsPointCalibrate == 1:
  382.   if IsMaskArea == 1:
  383.     for i in range(len(AllAreaPoints_List)):
  384.       length = len(AllAreaPoints_List[i])
  385.       AreaPointNum_List.append(length)
  386.     print AreaPointNum_List
  387.       
  388.     for i in range(len(AreaPointNum_List)):
  389.       MAreaPointNum.append(int(AreaPointNum_List[i]))
  390.       MAreaDirection.append(int(AreaDirection[i]))
  391.     print MAreaDirection,MAreaPointNum, IsPointCalibrate, IsRoadLine, IsCarStatisLine, IsMaskArea, AreaNum, AllAreaPoints_List
  392.      
  393.     point_sum = 0
  394.     for i in range(AreaNum):
  395.       data2 += struct.pack('@i', MAreaPointNum[i])
  396.       data3 += struct.pack('@i', MAreaDirection[i])
  397.       point_sum += MAreaPointNum[i]
  398.       for j in range(len(AllAreaPoints_List[i])):
  399.         data4 += struct.pack('@2i', AllAreaPoints_List[i][j]['x'], AllAreaPoints_List[i][j]['y'])
  400.     print point_sum
  401.       
  402.     for i in range(21):
  403.       data5 += struct.pack('@i', 0)
  404.     for i in range(4):
  405.       data6 += struct.pack('@2i', 0, 0)
  406.         
  407.     data2_in, data3_in, data4_in, data5_in = '', '', '', ''
  408.     if AreaNum < 10:
  409.       for i in range(10-AreaNum):
  410.         data2_in += struct.pack('@i', 0)
  411.         data3_in += struct.pack('@i', 0)
  412.       for i in range(100 - point_sum):
  413.         data4_in += struct.pack('@2i', 0, 0)
  414.       BinData = data1 + data2 + data2_in + data3 + data3_in + data5 + data4 + data4_in + data6
  415.     else:
  416.       BinData = data1 + data2 + data3 + data5 + data4 + data6
  417.       
  418.     dic['RoadOrArea'] = BinData
  419.     print "lb = %d\n"%(len(dic['RoadOrArea']))
  420.   
  421.   elif IsRoadLine == 1 or IsPointCalibrate == 1 or IsCarStatisLine == 1:
  422.     print AllPoints
  423.     L_Points = coordinate_scheduling(AllPoints)
  424.     print L_Points
  425.     for i in range(21):
  426.       data2 += struct.pack('@i',0)
  427.       
  428.     if IsPointCalibrate == 1:
  429.       for i in range(len(L_Points)):
  430.         x,y = L_Points[i]
  431.         data3 += struct.pack('@2i',x,y )
  432.       for i in range(220):
  433.         data4 += struct.pack('@i',0)
  434.       BinData = data1 + data2 + data3 + data4
  435.         
  436.       dic['AutoOrManuCoordinate'] = BinData
  437.       print "la = %d\n"%(len(dic['AutoOrManuCoordinate']))
  438.     
  439.     
  440.     elif IsRoadLine == 1:
  441.       for i in range(4):
  442.         data3 += struct.pack('@2i', 0, 0)
  443.       for i in range(len(L_Points)):
  444.         x,y = L_Points[i]
  445.         data4 += struct.pack('@2i', x, y)
  446.       for i in range(104):
  447.         data5 += struct.pack('@2i', 0 ,0)
  448.       BinData = data1 + data2 + data3 + data4 + data5
  449.         
  450.       dic['RoadOrArea'] = BinData
  451.       #print "lb = %d\n"%(len(dic['RoadOrArea']))

  452.     elif IsCarStatisLine == 1:
  453.       data2 = ''
  454.       for i in range(20):
  455.         data2 += struct.pack('@i', 0)
  456.       data3 = struct.pack('@i', CarLineNum)
  457.       for i in range(110):
  458.         data4 += struct.pack('@2i', 0, 0)

  459.       a_Line, xy = [], []
  460.       print len(AllLinePoints)
  461.       for i in range(len(AllLinePoints)):
  462.         a_Line.append(AllLinePoints[i])
  463.       for i in range(len(a_Line)):
  464.         xy = coordinate_scheduling(a_Line[i])
  465.         for i in range(len(xy)):
  466.           x,y = xy[i]
  467.           data5 += struct.pack('@2i', x, y)
  468.       if len(AllLinePoints) != 2:
  469.         data5_in = struct.pack('@4i', 0, 0, 0 ,0)
  470.         BinData = data1 + data2 + data3 + data4 + data5 + data5_in
  471.       else:
  472.         BinData = data1 + data2 + data3 + data4 + data5
  473.         
  474.       dic['CarLine'] = BinData
  475.       print "lc = %d\n"%(len(dic['CarLine']))
  476.       
  477.   elif IsPointCalibrate == 0:
  478.     for i in range(254):
  479.       BinData += struct.pack('@i',0)
  480.     dic['AutoOrManuCoordinate'] = BinData
  481.     print "la = %d\n"%(len(dic['AutoOrManuCoordinate']))
  482.     
  483.   print dic
  484.   a, b, c, d = [], [], [], []
  485.   all_Bin = ''
  486.   for i in range(254):
  487.     a.append(struct.unpack('@254i', dic['AutoOrManuCoordinate'])[i])
  488.     b.append(struct.unpack('@254i', dic['RoadOrArea'])[i])
  489.     c.append(struct.unpack('@254i', dic['CarLine'])[i])
  490.     d.append(a[i]^b[i]^c[i])
  491.     all_Bin += struct.pack('@i',d[i])

  492.   print d
  493.     
  494.   
  495.   
  496.   #backup
  497.   f = open(backup_filename, 'wb')
  498.   pickle.dump(all_Bin, f)
  499.   f.close()
  500.   
  501.   write_to_file(all_Bin)
  502.   
  503. #write to file
  504. def write_to_file(a):
  505.   f = open(file_name, 'wb')
  506.   f.write(a)
  507.   f.close()
  508.   
  509. def load(widget):
  510.   f = open(backup_filename, 'rb')
  511.   a = pickle.load(f)
  512.   f.close()
  513.   
  514.   write_to_file(a)
  515.   
  516. def remove(widget):
  517.   global dic
  518.   global IsPointCalibrate
  519.   global IsRoadLine
  520.   global IsMaskArea
  521.   global IsCarStatisLine
  522.   
  523.   IsPointCalibrate = 0
  524.   IsRoadLine = 0
  525.   IsMaskArea = 0
  526.   IsCarStatisLine = 0
  527.   
  528.   cmd = 'cat /dev/null > %s'%(file_name)
  529.   os.system(cmd)
  530.   dic = {'AutoOrManuCoordinate' : value, 'RoadOrArea' : value, 'CarLine' : value,}
  531.   
  532.   
  533. def save(widget):
  534.   print "save button clicked.\n"
  535.   
  536.   global IsPointCalibrate
  537.   global IsRoadLine
  538.   global IsMaskArea
  539.   global IsCarStatisLine
  540.   
  541.   save_data()
  542.   init_para()
  543.   reflash_image()
  544.   IsPointCalibrate = 0
  545.   IsRoadLine = 0
  546.   IsMaskArea = 0
  547.   IsCarStatisLine = 0
  548.   

  549. def init_para():
  550.   
  551.   global DrawPoint
  552.   global PointList
  553.   global DrawLinePoints_List
  554.   global AllAreaPoints_List
  555.   global AllLinePoints
  556.   global AreaNum
  557.   global AllPoints
  558.   global AreaPointNum_List

  559.   DrawPoint = 0
  560.   PointList = []
  561.   DrawLinePoints_List = []
  562.   AllAreaPoints_List = []
  563.   AllLinePoints = []
  564.   AllPoints = []
  565.   AreaNum = 0
  566.   AreaPointNum_List = []
  567.   
  568. if __name__ == '__main__':
  569.   
  570.   IsPointCalibrate = 0
  571.   IsRoadLine = 0
  572.   IsMaskArea = 0
  573.   IsCarStatisLine = 0
  574.   AreaNum = 0
  575.   AreaPointNum_List = []
  576.   AreaDirection = []
  577.   CarLineNum = 0

  578.   win = widgets(gdXML, PIC)
  579.   gtk.main()



阅读(2290) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~