У меня есть студент, который использует django
framework для создания приложения.
У нее есть шаблон, содержащий слайдер. Часть этой формы в шаблоне:
<form method="GET" enctype="multipart/form-data">
<div class="slidecontainer">
<input name="range" type="range" min="{{ beginframe }}" max="{{ endframe }}" onclick="sLider.oninput();" class="slider" id="range">
<p>Value: <span id="demo"></span></p>
<button type="button" class="button4" name="showframebtn" id="showframebtn" >Show frame</button>
</div>
<input type="hidden" name="fname1" value="{{video_path}}" style=" margin-top: 15px; margin-left: -45px;width: 410px; height: 40px;">
</form>
Затем ей нужно обработать значение ползунка и значение {{video_path}}
. Это ее код функции в views.py:
elif request.method == 'GET' and 'f' in request.GET:
if framenumber:
print(framenumber)
fname_1 = request.GET.get('fname1')
print("fname", fname_1)
Однако значение скрытого входного параметра не передается. Значение, которое она получает, составляет None
.
Используя page source
из браузера, мы видим, что действительно скрытый параметр имеет значение.
<form method="GET" enctype="multipart/form-data">
<div class="slidecontainer">
<input name="range" type="range" min="1" max="11" onclick="sLider.oninput();" class="slider" id="range">
<p>Value: <span id="demo"></span></p>
<button type="button" class="button4" name="showframebtn" id="showframebtn" >Show frame</button>
</div>
<input type="hidden" name="fname1" value="C:/Users/Username/PycharmProjects/thesis/static/3/actual_video.avi" style=" margin-top: 15px; margin-left: -45px;width: 410px; height: 40px;">
</form>
Не могли бы вы помочь нам, как это исправить? Это связано с тем, что она использует параметр кнопки?
Заранее спасибо!
Полный код функции из views.py можно найти здесь:
def video_analysis(request):
framenumber = request.GET.get('f', '')
print("framenumber ", framenumber)
global frame2
global tmp_path
global myfile
global filename
global img1
global img2
global video_table, motionFlow_table
video_table = []
angles =[]
motionFlow_table = []
video_table = np.empty([30, 400, 400, 3], np.dtype('uint8'))
motionFlow_table = np.empty([30, 400, 400, 3], np.dtype('uint8'))
times =0
if (request.session['doctor_id'] == ""):
return redirect('login')
times = times +1
path = STATIC_PATH
direct = request.session['directory']
p_id = request.session['p_id']
p_name = request.session['p_name']
p_lastname = request.session['p_lastname']
os.chdir(direct)
print(direct)
myfile = None
if request.method == 'POST' and 'myfile' in request.FILES:
myfile = request.FILES['myfile']
fs = FileSystemStorage()
filename = myfile.name
print(filename)
if (not os.path.exists(direct + "\\" + myfile.name)):
filename = fs.save(direct + "\\" + myfile.name, myfile)
print(filename)
path = direct + "\\" + myfile.name
print(path)
print(direct)
request.session['path'] = path
request.session['file'] = myfile.name
print(myfile.name)
uploaded_file_url = fs.url(filename) + "/static"
print(uploaded_file_url)
if request.session.has_key('path'):
path = request.session['path']
tmp_path = ""
for i in range(0, len(path)):
if (path[i] == "\\"):
tmp_path = tmp_path + '/'
else:
tmp_path = tmp_path + path[i]
print(tmp_path)
print(myfile)
if myfile != None:
print("not empty")
cap = cv2.VideoCapture(str(myfile))
if (cap.isOpened() == False):
print("Error opening video")
begin_frame = 1
count =1 #counter for frames
ret, frame1 = cap.read()
# Region of Interest - ROI
template = frame1[150:250,0:100]
video_table[count] = frame1 # save the video into array
cv2.imwrite(('frame %d.jpg' % count), frame1)
original = frame1
grayimg_1 = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)
cv2.imwrite('frame1gray.jpg', grayimg_1)
gray_img, topleft, bottomright = template_matching(grayimg_1,template )
print("topleft",topleft)
print("bottomright",bottomright)
cv2.imwrite("gray1.jpeg", grayimg_1)
if np.shape(frame1) == ():
print ("empty frame")
hsv = np.zeros_like(frame1)
hsv[..., 1] = 255
fourcc = cv2.VideoWriter_fourcc(*'DIVX')
fourcc1 = cv2.VideoWriter_fourcc(*'DIVX')
out = cv2.VideoWriter('output.avi', fourcc, 25.0, (500, 400))
out1 = cv2.VideoWriter('output1.avi', fourcc1, 25.0, (500, 400))
while (cap.isOpened()):
ret, frame2 = cap.read()
count =count+1
if ret != True:
break;
video_table[count] = frame2 # save video frames into table
cv2.imwrite(('frame %d.jpg' % count), frame2)
grayimg_2 = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY)
cv2.imwrite("gray %d.jpeg", grayimg_2)
gray_img, topleft, bottomright = template_matching(grayimg_2, template)
# Computes a dense optical flow using the Gunnar Farneback's algorithm.
flow = cv2.calcOpticalFlowFarneback(grayimg_1, grayimg_2, None, 0.5, 3, 15, 3, 5, 1.2, 0)
test_img = draw_flow(gray_img, flow,topleft,bottomright)
cv2.imwrite(("motionFlow_img %d.jpg" %count), motionFlow_table[count])
# Calculate the magnitude and angle of 2D vectors.
mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1])
angles.append(ang)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cv2.imwrite('opticalfb.jpeg', frame2)
grayimg_1 = grayimg_2
print("number of frames: ", count)
out.release()
out1.release()
cv2.destroyAllWindows()
cv2.waitKey(0) #press something to continue
end_frame = count
# Video Properties
# number of frames
video_length = cap.get(cv2.CAP_PROP_FRAME_COUNT)
total_frames = int(video_length)
print("total" , total_frames)
#frame rate
frame_rate = float(cap.get(cv2.CAP_PROP_FPS))
print(frame_rate)
#video duration
duration = round(float(video_length / frame_rate), 2)
print(duration)
data = np.array(ang)
name_of_file1 = filename.split("/")[-1]
name_of_file1 = p_id + "/" + "frame 1.jpg"
print(name_of_file1)
return render(request, 'video_analysis.html', { 'video_frame' : name_of_file1,
'firstframe' : name_of_file1,
'video': myfile,
'video_path':tmp_path,
'beginframe': int(begin_frame),
'endframe': int(end_frame),
'video_duration': duration})