Ask Your Question
0

ValueError: total size of new array must be unchanged

asked 2016-07-16 08:41:08 -0600

Hello there

I am building a OpenCv2 self driving RC car and want to train it With CNN (neural Network) first.The camera input is from a wireless Ip camera stream.The car has to move in different directions by tagging each frame with a control. eg:- left, right, forward and backward.

The problem is that when I execute the code I get the following error:-

Start collecting images... init done opengl support available doneu Traceback (most recent call last): File "collect_training_data.py", line 159, in <module> CollectTrainingData() File "collect_training_data.py", line 30, in __init__ self.collect_image() File "collect_training_data.py", line 66, in collect_image temp_array = roi.reshape(1, 38400).astype(np.float32) ValueError: total size of new array must be unchanged Segmentation fault (core dumped)

Here the Code:-

__author__ = 'zhengwang'

import urllib2 import numpy as np import cv2 import serial import pygame from pygame.locals import *

class CollectTrainingData(object):

def __init__(self):

    self.stream = urllib2.Request('http://192.168.42.129:8080/video')
    self.response = urllib2.urlopen(self.stream)
    self.bytes = bytes()

    # connect to a seral port
    self.ser = serial.Serial('/dev/ttyUSB0', 115200, timeout=1)
    self.send_inst = True

    # create labels
    self.k = np.zeros((4, 4), 'float')
    for i in range(4):
        self.k[i, i] = 1
    self.temp_label = np.zeros((1, 4), 'float')

    pygame.init()
    self.collect_image()

def collect_image(self):

    saved_frame = 0
    total_frame = 0

    # collect images for training
    print('Start collecting images...')
    e1 = cv2.getTickCount()
    image_array = np.zeros((1, 38400))
    label_array = np.zeros((1, 4), 'float')

    # stream video frames one by one
    try:
        self.bytes = ' '
        frame = 1
        while self.send_inst:
            self.bytes += self.response.read(1024)
            first = self.bytes.find('\xff\xd8')
            last = self.bytes.find('\xff\xd9')
            if first != -1 and last != -1:
                jpg = self.bytes[first:last + 2]
                self.bytes = self.bytes[last + 2:]
                image = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8), cv2.CV_LOAD_IMAGE_GRAYSCALE)

                # select lower half of the image
                roi = image[120:240, :]

                # save streamed images
                cv2.imwrite('training_images/frame{:>05}.jpg'.format(frame), image)

                #cv2.imshow('roi_image', roi)
                cv2.imshow('image', image)

                # reshape the roi image into one row array
                temp_array = roi.reshape(1, 38400).astype(np.float32)

                frame += 1
                total_frame += 1

                # get input from human driver
                for event in pygame.event.get():
                    if event.type == KEYDOWN:
                        key_input = pygame.key.get_pressed()

                        # complex orders
                        if key_input[pygame.K_UP] and key_input[pygame.K_RIGHT]:
                            print("Forward Right")
                            image_array = np.vstack((image_array, temp_array))
                            label_array = np.vstack((label_array, self.k[1]))
                            saved_frame += 1
                            self.ser.write(chr(6))

                        elif key_input[pygame.K_UP] and key_input[pygame.K_LEFT]:
                            print("Forward Left")
                            image_array = np.vstack((image_array, temp_array))
                            label_array = np.vstack((label_array, self.k[0]))
                            saved_frame += 1
                            self.ser.write(chr(7))

                        elif key_input[pygame.K_DOWN] and key_input[pygame.K_RIGHT]:
                            print("Reverse Right")
                            self.ser.write(chr(8))

                        elif key_input[pygame.K_DOWN] and key_input[pygame.K_LEFT]:
                            print("Reverse Left")
                            self.ser.write(chr(9))

                        # simple orders
                        elif key_input[pygame.K_UP]:
                            print("Forward")
                            saved_frame += 1
                            image_array = np.vstack((image_array, temp_array))
                            label_array = np.vstack((label_array, self.k[2]))
                            self.ser.write(chr(1))

                        elif ...
(more)
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-07-18 01:18:42 -0600

berak gravatar image

your roi is 120x240 == 28800, thus you can't reshape to 38400

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-07-16 08:41:08 -0600

Seen: 1,296 times

Last updated: Jul 16 '16