def drawNetworkWithHomography(self, H):
		edges = self.roadNetwork.edges
		nodes = self.roadNetwork.nodes
		for edge in edges:
			nodeI = edge.node1
			nodeJ = edge.node2
			width = edge.width
			### Prastutaniki angle em ledhu, only straight lines.
                        pts1 = np.array([[int(nodeI.x-width/2), nodeI.y], [int(nodeI.x-width/2), nodeJ.y]])
                        pts2 = np.array([[int(nodeI.x+width/2), nodeI.y], [int(nodeI.x+width/2), nodeJ.y]])
                        pts1 = transformSetOfPointsAndReturn(pts1, H)
                        pts2 = transformSetOfPointsAndReturn(pts2, H)
			cv2.line(self.grid, (pts1[0][0], pts1[0][1]), (pts1[1][0], pts1[1][1]), (255,0,0), 1)
			cv2.line(self.grid, (pts2[0][0], pts2[0][1]), (pts2[1][0], pts2[1][1]), (255,0,0), 1)
def simulateFromXML(video, xml, size, homography=False):
    frames , count = getAllFrames(xml)
    if video != None:
        cap = cv2.VideoCapture(video)
    for i, frame in enumerate(frames.keys()):
        vehicles = frames[frame]
        print "Frame Number ", i
        if video != None:
            ret, video_frame = cap.read()

        #print frames[frame]
        output = np.zeros(size)
        output_from_model = np.zeros(size)
        #cv2.putText(output, str(i), (20,20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (255,0,0), 1, cv2.CV_AA)
        if homography == True:
            straight_road_points = np.array([[0,0],[300,0],[300,710],[0,710]], dtype=float)
            perspective_road_points = np.array([[536,136], [615,136], [655,655], [0,475]], dtype=float)
            perspective_road_points1 = np.array([[536,136], [615,136], [655,655], [0,475], [ 414., 464], [549., 245]], dtype=float)
            straight_road_points1 = np.array([[0,0],[300,0],[300,710],[0,710], [150,0], [150,100] , [150,200], [150,300], [150, 400], [150, 500], [150, 600], [150, 700]], dtype=float)
            output = drawPoly(output, straight_road_points, [255,0,0])
            output = drawPoly(output, perspective_road_points, [0,0,255])
            video_frame = drawPoly(video_frame, straight_road_points, [255,0,0])
            video_frame = drawPoly(video_frame, perspective_road_points, [0,0,255])
            H, status = cv2.findHomography(perspective_road_points, straight_road_points)
            #H, status = cv2.findHomography(straight_road_points, perspective_road_points)
            straight_road_points2 = np.copy(straight_road_points1)
            #print perspective_road_points
            #for (x, y) in zip(straight_road_points2, transformSetOfPointsAndReturn(straight_road_points1 ,H)):
            #    print x, y
            #break
            vehicles_topView = np.copy(vehicles)
            vehicles_topView = changeVehicles(transformSetOfPointsAndReturn(getVehicleCoordinates(vehicles), H), vehicles_topView) 
            print vehicles, transformSetOfPointsAndReturn(getVehicleCoordinates(vehicles), H), vehicles_topView
        output = drawVehicles(output, vehicles)

        if homography == True:
            output = drawVehicles_red(output, vehicles_topView)
        output = output[::-1,:,:]
        #output = output[:,::-1,:]
        cv2.imshow("sim", output)
        if video != None:
            cv2.imshow("real", video_frame)
        if cv2.waitKey(33) == 27:
            break
    cv2.destroyAllWindows()
Пример #3
0
def simulateFromXML(video, xml, size, homography=False):
    frames, count = getAllFrames(xml)
    if video != None:
        cap = cv2.VideoCapture(video)
    prev_vehicles = np.array([])
    simulated_vehicles = None
    accelerating_vehicles = {}
    left_vehicles = []
    prev_speeds = {}
    for i, frame in enumerate(frames.keys()):
        vehicles = frames[frame]
        print "Frame Number ", i, len(vehicles)

        if video != None:
            ret, video_frame = cap.read()

        #print frames[frame]
        output = np.zeros(size)
        cv2.putText(output, str(i), (20, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.75,
                    (255, 0, 0), 1, cv2.CV_AA)
        output_from_model = np.zeros(size)
        if homography == True:
            straight_road_points = np.array(
                [[0, 0], [300, 0], [300, 710], [0, 710]], dtype=float)
            perspective_road_points = np.array(
                [[536, 136], [615, 136], [655, 655], [0, 475]], dtype=float)
            perspective_road_points1 = np.array(
                [[536, 136], [615, 136], [655, 655], [0, 475], [414., 464],
                 [549., 245]],
                dtype=float)
            straight_road_points1 = np.array(
                [[0, 0], [300, 0], [300, 710], [0, 710], [150, 0], [150, 100],
                 [150, 200], [150, 300], [150, 400], [150, 500], [150, 600],
                 [150, 700]],
                dtype=float)
            output = drawPoly(output, straight_road_points, [255, 0, 0])
            output = drawPoly(output, perspective_road_points, [0, 0, 255])
            video_frame = drawPoly(video_frame, straight_road_points,
                                   [255, 0, 0])
            video_frame = drawPoly(video_frame, perspective_road_points,
                                   [0, 0, 255])
            H, status = cv2.findHomography(perspective_road_points,
                                           straight_road_points)
            #H, status = cv2.findHomography(straight_road_points, perspective_road_points)
            straight_road_points2 = np.copy(straight_road_points1)
            #print perspective_road_points
            #for (x, y) in zip(straight_road_points2, transformSetOfPointsAndReturn(straight_road_points1 ,H)):
            #    print x, y
            #break
            vehicles_topView = np.copy(vehicles)
            vehicles_topView = changeVehicles(
                transformSetOfPointsAndReturn(getVehicleCoordinates(vehicles),
                                              H), vehicles_topView)
            print vehicles, transformSetOfPointsAndReturn(
                getVehicleCoordinates(vehicles), H), vehicles_topView
        #output = drawVehicles(output, vehicles)

        for vehicle in vehicles:
            for veh in prev_vehicles:
                if vehicle[0] == veh[0]:
                    print "difference ", veh, vehicle, abs(veh[4] - vehicle[4])
                    if abs(veh[4] - vehicle[4]) > 2:
                        if veh[0] not in accelerating_vehicles:
                            accelerating_vehicles[veh[0]] = 1
                        else:
                            accelerating_vehicles[veh[0]] += 1
                        #vehicle[5] = vehicle[4] - veh[4]
                        #vehicle[5] = 0
                    else:
                        vehicle[5] = 0

        if i == 0:
            simulated_vehicles = np.copy(vehicles)
        else:
            simulated_vehicles1 = np.copy(vehicles)
            print "Simulated Vehicles 1 ", simulated_vehicles
            vehicles_inframe = []
            for j, veh in enumerate(simulated_vehicles1):
                for v in simulated_vehicles:
                    if v[0] == veh[0] and (v[4] != 0):
                        #veh[1] = v[1]
                        if v[4] + veh[5] <= 17:
                            #if v[4] + veh[5] > -1:
                            veh[2] = v[2] + veh[5]
                            veh[4] = v[4] + veh[5]
                            #veh[5] = v[5]
                        else:
                            veh[2] = v[2]
                            veh[4] = 16
                if veh[2] <= 680 and veh[0] not in left_vehicles:
                    vehicles_inframe.append(j)
                elif veh[2] > 680:
                    left_vehicles.append(veh[0])
            print "inframe ", vehicles_inframe, len(
                vehicles_inframe), simulated_vehicles1.shape
            print "before ", simulated_vehicles1
            if simulated_vehicles1.size != 0:
                simulated_vehicles1 = simulated_vehicles1[vehicles_inframe, :]
            print "after ", simulated_vehicles1
            simulated_vehicles = simulated_vehicles1

        output, simulated_vehicles, prev_speeds = drawVehiclesWithSimulation(
            output, vehicles, prev_vehicles, simulated_vehicles, prev_speeds)
        prev_vehicles = np.copy(vehicles)

        #output = drawVehicles(output, vehicles, 0)

        if homography == True:
            output = drawVehicles_red(output, vehicles_topView)
        output = output[::-1, :, :]
        #output = output[:,::-1,:]
        cv2.imshow("sim", output)
        print "Number of vehicles accelerating ", len(
            accelerating_vehicles.keys())
        if video != None:
            cv2.imshow("real", video_frame)
        print "Accelerating vehicles ", accelerating_vehicles
        print "Number is ", len(accelerating_vehicles.keys())
        if cv2.waitKey(33) == 27:
            break
    cv2.destroyAllWindows()