"This code is unreachable" warning in pycharm

370 views Asked by At

The following part of my code is highlighted as code unreachable in pycharm IDE.

 for row in range(frame_height):
                for col in range(frame_width):
                    if i < binary_img.shape[0]:
                        pixel = frame[row][col]
                        binary_pixel = np.unpackbits(np.array(pixel).astype(np.uint8))
                        binary_pixel[-1] = binary_img[i][-1]
                        pixel = np.packbits(binary_pixel).astype(np.int32)
                        frame[row][col] = pixel
                        i += 1
                else:
                    break
 cv2.imshow('Hidden Video', frame)
 if cv2.waitKey(1) & 0xFF == ord('q'):
    break

It is working perfectly, but there is a warning for this code saying that it is unreachable.

1

There are 1 answers

0
Heeya On

Thank you @pavel and @mous, I looked at the code again and I found out that there was a problem with the indentation in the outer for loop. It should have been like this:

# Iterate through each pixel in the video and hide a bit of the image in it
i = 0
while cap.isOpened():
    ret, frame = cap.read()
    if not ret:
        break
    for row in range(frame_height): #indentation error was in this line
        for col in range(frame_width):
            if i < binary_img.shape[0]:
                pixel = frame[row][col]
                binary_pixel = np.unpackbits(np.array(pixel).astype(np.uint8))
                binary_pixel[-1] = binary_img[i][-1]
                pixel = np.packbits(binary_pixel).astype(np.int32)
                frame[row][col] = pixel
                i += 1
            else:
                break
    cv2.imshow('Hidden Video', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break