progress bar python pyqt5
Умные головы, подскажите как правильно задать значения прогресс бару, при делении видео на кадры получается 700+ кадров например, значение cnt со 100 кадров сразу заполняет прогресс бар на 100. здесь я пытаюсь задать прогресс бар:
for i in range(len(video_colors)):
video_colors[i] |= image_bits[i]
video_colors = np.reshape(np.asarray(video_colors), (req_frame_count, vid_height, vid_width, 3)).astype(np.uint8)
# create dirs
if os.path.exists("temp"):
remove_directory_content("temp")
else:
make_dir("temp")
cnt = 1
for frame in video_colors:
cv2.imwrite('./temp/frame{:09d}.png'.format(cnt), frame, [cv2.IMWRITE_PNG_COMPRESSION, 0])
cnt += 1
print('Starting write back...')
while True:
ret, frame = cap.read()
if ret:
cv2.imwrite('./temp/frame{:09d}.png'.format(cnt), frame, [cv2.IMWRITE_PNG_COMPRESSION, 0])
cnt += 1
w.progressBar.setValue(cnt) # progressbar
else:
break
ПОЛНЫЙ КОД:
def hide(carrier_video, image_message):
cap = cv2.VideoCapture(carrier_video)
msg = cv2.imread(image_message)
vid_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
vid_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
vid_fps = cap.get(cv2.CAP_PROP_FPS)
# count = 0
# TIME_LIMIT=100
req_frame_count = required_frame_count(cap, msg)
bin_rep_width = bin_rep(msg.shape[1]).zfill(16)
bin_rep_height = bin_rep(msg.shape[0]).zfill(16)
bin_rep_mult = bin_rep(msg.shape[0] * msg.shape[1]).zfill(32)
image_bits = [int(x == '1') for x in bin_rep_width + bin_rep_height + bin_rep_mult]
for row in msg:
for column in row:
for color in column:
for i in range(8):
image_bits.append(int((color & (1 << (7 - i))) != 0))
video_colors = []
for i in range(req_frame_count):
ret, frame = cap.read()
if not ret:
raise Exception('Error in extracting video frames')
for row in frame:
for column in row:
for color in range(3):
video_colors.append(column[color])
assert len(video_colors) >= len(image_bits)
for i in range(len(video_colors) - len(image_bits)):
image_bits.append(0)
video_colors = [(color & 0xFE) for color in video_colors]
for i in range(len(video_colors)):
video_colors[i] |= image_bits[i]
video_colors = np.reshape(np.asarray(video_colors), (req_frame_count, vid_height, vid_width, 3)).astype(np.uint8)
# create dirs
if os.path.exists("temp"):
remove_directory_content("temp")
else:
make_dir("temp")
cnt = 1
for frame in video_colors:
cv2.imwrite('./temp/frame{:09d}.png'.format(cnt), frame, [cv2.IMWRITE_PNG_COMPRESSION, 0])
cnt += 1
print('Starting write back...')
while True:
ret, frame = cap.read()
if ret:
cv2.imwrite('./temp/frame{:09d}.png'.format(cnt), frame, [cv2.IMWRITE_PNG_COMPRESSION, 0])
cnt += 1
w.progressBar.setValue(cnt) # progressbar
else:
break
Ответы (2 шт):
Автор решения: Sergey Tatarincev
→ Ссылка
по коду не вполне ясно, но что мешает
w.progressBar.setMaximum(700) # или сколько вам надо
и устанавливайте cnt 100-200-300... прогрессбар проценты сам подсчитает
Автор решения: Batyr
→ Ссылка
Вот алгоритм прогресс бара. Какую число вы не писали он всегда показывает выполнения прогресса в проценте.
def bp():
input_integer = int((ui.lineEdit.text()))
a = 0
while a <= input_integer:
x = 100 / input_integer
x = x * a
a = a + 1
ui.progressBar.setValue(x)