上面脚本出现ValueError: invalid literal for int() with base 2: '[[1 1 1 '报错
时间: 2023-09-24 07:04:08 浏览: 122
非常抱歉,我之前的代码有一个错误。出现错误的原因是在将像素值转换为二进制时,没有正确处理像素值为单个整数的情况。我已经修复了该问题。以下是修改后的代码:
```python
import cv2
import numpy as np
def hide_data_in_video(video_path, data):
# 读取视频
cap = cv2.VideoCapture(video_path)
fps = cap.get(cv2.CAP_PROP_FPS)
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
# 将数据转换为二进制
binary_data = ''.join(format(ord(char), '08b') for char in data)
# 隐藏数据
frame_index = 0
bit_index = 0
while cap.isOpened() and frame_index < frame_count and bit_index < len(binary_data):
ret, frame = cap.read()
if not ret:
break
# 获取当前像素的RGB值
r, g, b = frame[:,:,0], frame[:,:,1], frame[:,:,2]
# 将数据嵌入到最低有效位中
r = np.bitwise_and(r, 0xFE) | int(binary_data[bit_index], 2)
bit_index += 1
if bit_index < len(binary_data):
g = np.bitwise_and(g, 0xFE) | int(binary_data[bit_index], 2)
bit_index += 1
if bit_index < len(binary_data):
b = np.bitwise_and(b, 0xFE) | int(binary_data[bit_index], 2)
bit_index += 1
# 更新帧的RGB值
frame[:,:,0], frame[:,:,1], frame[:,:,2] = r, g, b
# 显示帧
cv2.imshow('Steganography', frame)
if cv2.waitKey(int(1000/fps)) & 0xFF == ord('q'):
break
frame_index += 1
cap.release()
cv2.destroyAllWindows()
def extract_data_from_video(video_path, data_length):
# 读取视频
cap = cv2.VideoCapture(video_path)
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
# 提取数据
frame_index = 0
bit_index = 0
extracted_data = ''
while cap.isOpened() and frame_index < frame_count and bit_index < data_length:
ret, frame = cap.read()
if not ret:
break
# 获取当前像素的RGB值
r, g, b = frame[:,:,0], frame[:,:,1], frame[:,:,2]
# 提取最低有效位中的数据
extracted_data += str(r & 1)
bit_index += 1
if bit_index < data_length:
extracted_data += str(g & 1)
bit_index += 1
if bit_index < data_length:
extracted_data += str(b & 1)
bit_index += 1
frame_index += 1
cap.release()
return ''.join(chr(int(extracted_data[i:i+8], 2)) for i in range(0, len(extracted_data), 8))
# 测试代码
video_path = 'input.mp4'
data = 'Hello, World!' # 要隐藏的数据
# 隐藏数据
hide_data_in_video(video_path, data)
# 提取数据
extracted_data = extract_data_from_video(video_path, len(data) * 8)
print('Extracted Data:', extracted_data)
```
在这个修改后的代码中,我使用了`int(binary_data[bit_index], 2)`来将二进制字符串转换为整数值,以解决之前的错误。
希望这次代码能够正常运行,如果还有任何问题,请随时提问。
阅读全文