0

I'm trying to link my IP camera to my a AWS service and there's 2 ways I can do this, either with my built in computer camera (runs fine) and a IP camera. The code I'm using is from https://github.com/aws-samples/amazon-rekognition-video-analyzer which is writing in Python 2.7(but I'm doing it in python 3), I already converted the code to python 3 (using python 2to3).but when I run the code I keep getting this error of only concatenate strings not bytes:

I'm new to python so for what I have research is that 2to3 will do must of the work, but I'm pretty sure this part of converting bytes to strings is not in there and I'm not sure how to handle this conversion/parsing.

Traceback (most recent call last):
  File "video_cap_ipcam.py", line 140, in <module>
    main()
  File "video_cap_ipcam.py", line 104, in main
    bytes += stream.read(16384*2)
TypeError: can only concatenate str (not "bytes") to str

video_cap_ipcam.py file:

def main():

    ip_cam_url = ''
    capture_rate = default_capture_rate
    argv_len = len(sys.argv)

    if argv_len > 1:
        ip_cam_url = sys.argv[1]
        
        if argv_len > 2 and sys.argv[2].isdigit():
            capture_rate = int(sys.argv[2])
    else:
        print("usage: video_cap_ipcam.py <ip-cam-url> [capture-rate]")
        return

    print(("Capturing from '{}' at a rate of 1 every {} frames...".format(ip_cam_url, capture_rate)))
    stream = urllib.request.urlopen(ip_cam_url)
    
    bytes = ''
    pool = Pool(processes=3)

    frame_count = 0
    while True:
        # Capture frame-by-frame
        frame_jpg = ''

        bytes += stream.read(16384*2)
        b = bytes.rfind('\xff\xd9')
        a = bytes.rfind('\xff\xd8', 0, b-1)


        if a != -1 and b != -1:
            #print 'Found JPEG markers. Start {}, End {}'.format(a,b)
            
            frame_jpg_bytes = bytes[a:b+2]
            bytes = bytes[b+2:]

            if frame_count % capture_rate == 0:
                
            
                img_cv2_mat = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
                rotated_img = cv2.transpose(cv2.flip(img_cv2_mat, 0))
                
                
                retval, new_frame_jpg_bytes = cv2.imencode(".jpg", rotated_img)

                #Send to Kinesis
                result = pool.apply_async(send_jpg, (bytearray(new_frame_jpg_bytes), frame_count, True, False, False,))

            frame_count += 1

if __name__ == '__main__':
    main()

1 Answer 1

2

When you initially set the variable bytes to '', the variable becomes a string, which in Python 3 is considered a sequence of characters rather than a sequence of bytes. (A character can be expressed using multiple bytes.)

If you want bytes to be a sequence of bytes, initialize it as b'' instead. Then you can concatenate further bytes to it.

Sign up to request clarification or add additional context in comments.

8 Comments

can you pleased detailed your explanation,i already initialized it as '''b ' ' ''' and it throws a syntax error
There's not supposed to be space between the b and the ''.
Also, there shouldn't be a space between each "'" in "''", since it's supposed to be an empty string.
Thanks,its starting to make sense but for slice notation is it posible? rame_jpg_bytes = bytes[a:b+2] i already tried frame_jpg_bytes =[a:b+2].encode()
Slice notation works with byte sequences. There are two problems with [a:b+2].encode(). The first is that you're missing a variable name in front of the "[", and the second is that byte sequences do not have an encode() method.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.