問題:請問kivy圖片的”source“可以用變數賦值嗎?如果可以的話,應該怎么做?
如圖,當我直接給source賦值字串時:source:”HDR/fusion_xxx.png“時可以正常顯示,
但是當我用變數賦值時,不能正常顯示。

全部代碼:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.uix.image import Image
from kivy.lang import Builder
from kivy.uix.label import Label
import time
import kivy
import cv2 as cv
import numpy as np
import argparse
import os
import cv2
from kivy.clock import Clock
from kivy.graphics.texture import Texture
from kivy.core.window import Window
import pygame.camera
import pygame.image
Builder.load_string("""
<CameraScreen>:
BoxLayout:
orientation: 'vertical'
Camera:
id: sjnCamera
play: True
index: 0
width:'60dp'
height:'80dp'
# Preview:
# id: qrcam
BoxLayout:
orientation:'horizontal'
size_hint:1,.1
Button:
text:'camera0'
background_color: 1,1,0,1
on_press: root.selectCamera(0)
Button:
text:'camera1'
background_color: 1,1,0,1
on_press: root.selectCamera(1)
Button:
text:'camera2'
background_color: 1,1,0,1
on_press: root.selectCamera(2)
Button:
text:'camera3'
background_color: 1,1,0,1
on_press: root.selectCamera(3)
BoxLayout:
orientation:'horizontal'
size_hint:1,.1
Button:
text:'1 stop'
background_color: 1,1,0,1
on_press: root.selectCompensation(1)
Button:
text:'2 stop'
background_color: 1,1,0,1
on_press: root.selectCompensation(2)
Button:
text:'3 stop'
background_color: 1,1,0,1
on_press: root.selectCompensation(3)
BoxLayout:
orientation:'horizontal'
size_hint_y: None
height: '48dp'
Button:
text: 'Display'
on_press: root.manager.current = 'photo_screen'
Button:
text: 'Shoot'
on_press: root.takePicture()
Button:
text: 'createCamera'
#on_press: root.createCamera()
<PhotoScreen>:
display_HDR_image:"/HDR/fusion_175225.png"
BoxLayout:
orientation: 'vertical'
# Image:
# source:root.display_HDR_image
# id:HDR_display
# size_hint:1,.9
Button:
id:btn1
size_hint:1,.9
#dhi:"/HDR/fusion_175225.png"
canvas.after:
Rectangle:
id:HDR_display
pos:self.pos
size:self.size
source:root.display_HDR_image
#source:self.dhi
#source:"HDR/fusion_175225.png"
# Image:
# width:'60dp'
# height:'80dp'
# source:root.display_HDR_image
# Image:
# id:HDR_display
# size_hint:1,.9
# allow_stretch:True
# source:root.display_HDR_image
# canvas:
# Rectangle:
# #size_hint:1,.9
# #size:self.width+20,self.height+20
# pos:self.pos
# source:root.display_HDR_image
# Preview:
# id: qrcam
# canvas:
# color:
# rgba:[1,1,1,1]
# Rectangle:
# size:self.width+20,self.height+20
# pos:self.pos
# source:root.display_HDR_image
BoxLayout:
orientation:'horizontal'
size_hint:1,.1
Button:
text: 'left'
on_press: root.left()
Button:
text: 'right'
on_press: root.right()
Button:
text: 'Back'
on_press:
root.exit_display()
root.manager.current = 'camera_screen'
""")
# Declare both screens
class CameraScreen(Screen):
global camera_id # 選擇攝像頭
camera_id = 0
global preview_state # 控制預覽是否開啟
preview_state = True
global compensation # 曝光補償
compensation = 1
# global camera
# camera = None
# global frame
def selectCamera(self, i):
"""選擇攝像頭"""
global camera_id
camera_id = i
print("selected camera " + str(camera_id))
# camera = self.index = camera_id
return camera_id
def selectCompensation(self, i):
"""回應設定的曝光補償"""
global compensation
compensation = i
print("selected compensation " + str(compensation))
return compensation
def takePicture(self):
global compensation
global camera_id
global camera
pic_list = []
take_picture_number = 3
# self.ids['sjnCamera'].play = False
cam = cv2.VideoCapture(camera_id)
exposure_list = self.getExposureList(cam, compensation) # 獲得曝光序列
cwd = os.getcwd()
while take_picture_number:
for t in exposure_list:
cam.set(cv2.CAP_PROP_EXPOSURE, t)
ret, frame_read = cam.read()
if ret:
time_str = time.strftime("%H%M%S")
picture_name = "IMG_{}".format(time_str) + "_" + str(take_picture_number) + ".jpg"
save_path = cwd + "/LDR/" + picture_name
cv2.imwrite(save_path, frame_read)
pic_list.append(picture_name) # 圖片名稱序列
print('保存影像成功')
print("camera_id:" + str(camera_id))
take_picture_number -= 1
# cv2.waitKey(1) # 延時1ms
else:
print("獲取失敗")
break
cam.release()
cam.set(cv2.CAP_PROP_AUTO_EXPOSURE, 0.75) # 設定回自動曝光
file_name = self.writeList(cwd, pic_list, exposure_list) # 寫入txt
self.createHDR(cwd, file_name, time_str) # 生成HDR
# def createCamera(self):
# global camera_id
# global camera
# camera = cv2.VideoCapture(camera_id)
# # 呼叫preview的start,和update
# #self.ids.qrcam.start(camera) # 呼叫preview類的start,獲取預覽
# return camera
def getExposureList(self, cam, compensation):
exposure_list = []
auto_exposure_time = 1 / float(cv2.CAP_PROP_EXPOSURE) # 獲取標準曝光時間
cam.set(cv2.CAP_PROP_AUTO_EXPOSURE, 0.25) # 切換為手動曝光
if compensation == 2:
exposure_time_m = auto_exposure_time / 4
exposure_time_p = auto_exposure_time * 4
exposure_list.append(exposure_time_m) # 曝光時間序列
exposure_list.append(auto_exposure_time)
exposure_list.append(exposure_time_p)
elif compensation == 3:
exposure_time_m = auto_exposure_time / 8
exposure_time_p = auto_exposure_time * 8
exposure_list.append(exposure_time_m) # 曝光時間序列
exposure_list.append(auto_exposure_time)
exposure_list.append(exposure_time_p)
else:
exposure_time_m = auto_exposure_time / 2
exposure_time_p = auto_exposure_time * 2
exposure_list.append(exposure_time_m) # 曝光時間序列
exposure_list.append(auto_exposure_time)
exposure_list.append(exposure_time_p)
return exposure_list
def loadExposureSeq(self, path, file_name):
"""載入曝光序列"""
images = []
times = []
list_path = path+"/List/"
LDR_path = path+"/LDR/"
file_path = os.path.join(list_path, file_name)
with open(file_path) as f: # 打開txt檔案,按行讀入到content
content = f.readlines()
for line in content: # 按行讀取
tokens = line.split() # 存為陣列
images.append(cv.imread(os.path.join(LDR_path, tokens[0])))
times.append(1 / float(tokens[1]))
return images, np.asarray(times, dtype=np.float32)
def writeList(self, path, frame_list, exposure_list):
path = path+"/List/"
list_name_index = time.strftime("%H%M%S")
file = list_name_index + '.txt'
i = len(frame_list)
f_path = os.path.join(path, file)
with open(os.path.join(path, file), 'a') as f:
while i:
f.write(str(frame_list[3 - i]) + ' ' + str(exposure_list[3 - i]) + '\n')
i -= 1
print("writeList 成功 ---")
return file
def createHDR(self, cwd, file_name, time_str):
images, times = self.loadExposureSeq(cwd, file_name) # 讀取圖形名稱和曝光時間
alignMTB = cv2.createAlignMTB() # 創建中值閾值位圖
alignMTB.process(images, images)
calibrate = cv.createCalibrateDebevec() # 獲得回應曲線
response = calibrate.process(images, times)
merge_debevec = cv.createMergeDebevec() # 合并影像
hdr = merge_debevec.process(images, times, response)
tonemap = cv.createTonemap(2.2) # 映射
ldr = tonemap.process(hdr)
merge_mertens = cv.createMergeMertens()
fusion = merge_mertens.process(images)
fusion_picture_name = "fusion_{}".format(time_str) + ".png"
save_path = cwd+"/HDR/"+fusion_picture_name
cv.imwrite(save_path, fusion * 255)
print("HDR創建成功---")
file = "HDR_list" + '.txt' # 20210522 -add 每生成一張HDR圖片,就在HDR_list中插入名字
HDR_list_path = cwd+"/HDR/"+fil
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/285288.html
標籤:Android
上一篇:致CSDN論壇用戶
