How to integrate OpenCV webcam with Django and React? - python

I have a barcode reader which I implemented by using opensource Dynamsoft API and OpenCV.
Now I need to integrate it with Django and display on my website in React.
I have no idea how to do that, I tried passing the code to my views.py but don't know what I should do next.
Here is my code for barcode reading:
import cv2
from dbr import *
import time
reader = BarcodeReader()
def text_results_callback_func(frame_id, t_results, user_data):
print(frame_id)
for result in t_results:
text_result = TextResult(result)
print("Barcode Format : ")
print(text_result.barcode_format_string)
print("Barcode Text : ")
print(text_result.barcode_text)
print("Exception : ")
print(text_result.exception)
print("-------------")
def get_time():
localtime = time.localtime()
capturetime = time.strftime("%Y%m%d%H%M%S", localtime)
return capturetime
def read_barcode():
video_width = 0
video_height = 0
vc = cv2.VideoCapture(0)
video_width = vc.get(cv2.CAP_PROP_FRAME_WIDTH)
video_height = vc.get(cv2.CAP_PROP_FRAME_HEIGHT)
vc.set(3, video_width)
vc.set(4, video_height)
stride = 0
if vc.isOpened():
rval, frame = vc.read()
stride = frame.strides[0]
else:
return
windowName = "Barcode Reader"
parameters = reader.init_frame_decoding_parameters()
parameters.max_queue_length = 30
parameters.max_result_queue_length = 30
parameters.width = video_width
parameters.height = video_height
parameters.stride = stride
parameters.image_pixel_format = EnumImagePixelFormat.IPF_RGB_888
parameters.region_top = 0
parameters.region_bottom = 100
parameters.region_left = 0
parameters.region_right = 100
parameters.region_measured_by_percentage = 1
parameters.threshold = 0.01
parameters.fps = 0
parameters.auto_filter = 1
reader.start_video_mode(parameters, text_results_callback_func)
while True:
cv2.imshow(windowName, frame)
rval, frame = vc.read()
if rval == False:
break
try:
ret = reader.append_video_frame(frame)
except:
pass
key = cv2.waitKey(1)
if key == ord('q'):
break
reader.stop_video_mode()
cv2.destroyWindow(windowName)
print("-------------------start------------------------")
reader.init_license("***************************")
read_barcode()

I think you need to use the JS version of Dynamsoft Barcode Reader to scan barcodes using cameras in a webpage: https://www.dynamsoft.com/barcode-reader/sdk-javascript/

Related

Data not recorded from second item onwards in excel using Python

I am currently doing a inventory management system using python and excel. The first item records just fine, but when I try scanning the 2nd item in the list it keeps saying "Item has been borrowed" eventhough its not borrowed yet. Each item has a unique QR code too.
Below is my code:
#CODE FOR BORROW
import PySimpleGUI as sg
import pandas as pd
import cv2
import pyzbar.pyzbar as zbar
import time
import datetime
import openpyxl
import subprocess
from openpyxl import load_workbook, Workbook
sg.popup('Please scan your Employee ID', title = "Borrow")
cap = cv2.VideoCapture(0)
font = cv2.FONT_HERSHEY_SIMPLEX
stopped = False
l=0
while(True):
key = cv2.waitKey(1)
if key == 27:
break
ret = cv2.waitKey(1) & 0xFF
# Capture frame-by-frame
ret, frame = cap.read()
decodedObjects = zbar.decode(frame)
if len(decodedObjects) > 0:
stopped = True
for obj in decodedObjects:
Employee_ID = obj.data.decode("utf-8")
cv2.putText(frame,Employee_ID, (50, 100), font, 2, (0, 0, 255), 3)
l+=1
# Display the resulting frame
cv2.imshow('Scan Employee ID',frame)
if l == 1:
cv2.waitKey(1)
time.sleep(2)
cv2.destroyAllWindows()
break
sg.popup('Please scan your Item ID', title = "Borrow")
cap = cv2.VideoCapture(0)
font = cv2.FONT_HERSHEY_SIMPLEX
stopped = False
l=0
while(True):
ret = cv2.waitKey(1) & 0xFF
# Capture frame-by-frame
ret, frame = cap.read()
decodedObjects = zbar.decode(frame)
if len(decodedObjects) > 0:
stopped = True
for obj in decodedObjects:
Borrow = obj.data.decode("utf-8")
cv2.putText(frame,Borrow, (50, 100), font, 2, (0, 0, 255), 3)
l+=1
# Display the resulting frame
cv2.imshow('Scan Item ID',frame)
if l == 1:
cv2.waitKey(1)
time.sleep(2)
cv2.destroyAllWindows()
break
wb=openpyxl.load_workbook(filename = r"C:\PROJECT\RIS\RIS\RIS.xlsx")
ws = wb.active
search_item = Borrow
for i in range(1, ws.max_row + 1):
for j in range(1, ws.max_column + 1):
if search_item == ws.cell(i,j).value:
location=(ws.cell(i,j+3).value)
if location:
# Define Excel File Path
RIS = r"C:\PROJECT\RIS\RIS\RIS.xlsx"
df_RIS = pd.read_excel(RIS)
time = datetime.datetime.now().strftime('%d-%m-%Y %H:%M:%S')
df_RIS.loc[df_RIS.SERIAL_NUMBER == Borrow, ['DATE_BORROW', 'STATUS_BORROW','DATE_RETURN', 'STATUS_RETURN']] = time, Employee_ID, "", ""
df_RIS.to_excel(RIS, index=False)
sg.popup('Borrowed item has been recorded successfully!', title = "Access Granted !")
subprocess.call([r"C:\PROJECT\RIS\RIS\RIS_test1.bat"])
else:
sg.popup('The above item has been borrowed.', title = "Error !")
My excel file for the item data :
Can I know how to resolve this? Thanks so much.
I tried naming the item under category differently but still dosent solve anything.

Having problem in video stegano, the hiden message always lost

friend, i am currently looking for a way to video stegano. I successfully in splitting frames from video file and hide messages inside them. But when i combine these frames into video and trying to extract info from the hiden video, i always failed. I guess here is problem with video compression.
Here is my code.
from stegano import lsb
from os.path import isfile, join
import time # install time ,opencv,numpy modules
import cv2
import numpy as np
import math
import os
import shutil
from moviepy.editor import *
from subprocess import call, STDOUT
def split_string(s_str, count=10):
per_c = math.ceil(len(s_str)/count)
c_cout = 0
out_str = ''
split_list = []
for s in s_str:
out_str += s
c_cout += 1
if c_cout == per_c:
split_list.append(out_str)
out_str = ''
c_cout = 0
if c_cout != 0:
split_list.append(out_str)
return split_list
def frame_extraction(video):
if not os.path.exists("./tmp"):
os.makedirs("tmp")
temp_folder = "./tmp"
print("[INFO] tmp directory is created")
vidcap = cv2.VideoCapture(video)
count = 0
while True:
success, image = vidcap.read()
if not success:
break
cv2.imwrite(os.path.join(temp_folder, "{:d}.png".format(count)), image)
count += 1
print("[INFO] frame {} is extracted".format(count))
def encode_string(input_string, root="./tmp/"):
split_string_list = split_string(input_string)
for i in range(0, len(split_string_list)):
f_name = "{}{}.png".format(root, i)
secret_enc = lsb.hide(f_name, split_string_list[i])
secret_enc.save(f_name)
print("[INFO] frame {} holds {}".format(f_name, lsb.reveal(f_name)))
def decode_string(video):
frame_extraction(video)
secret = []
root = "./tmp/"
for i in range(len(os.listdir(root))):
f_name = "{}{}.png".format(root, i)
print("[INFO] frame {} is decoding".format(f_name))
secret_dec = lsb.reveal(f_name)
if secret_dec == None:
break
secret.append(secret_dec)
print("[INFO] secret is {}".format("".join(secret)))
print(''.join([i for i in secret]))
# clean_tmp()
def clean_tmp(path="./tmp"):
if os.path.exists(path):
shutil.rmtree(path)
print("[INFO] tmp files are cleaned up")
def main():
input_string = input("Enter the input string: ")
f_name = input("enter the name of video: ")
# 从源文件分离出帧
frame_extraction(f_name)
# 分离文件路径和扩展名
file_path, file_extraction = os.path.splitext(f_name)
# 创建输出音频文件
audio_path = file_path + "_temp.mp3"
video = VideoFileClip(f_name)
video.audio.write_audiofile(audio_path)
# 加密字符
encode_string(input_string)
# 从tmp文件夹的图片创建没有声音的视频
fps=30
img_root = r"./tmp/"
# fourcc = cv2.VideoWriter_fourcc(*'mp4v')
fourcc = cv2.VideoWriter_fourcc(*'XVID')
video_file_path = file_path + "_temp.avi"
# 获取tmp文件夹第一张视频的尺寸
img = cv2.imread(img_root + "0.png")
height, width, layers = img.shape
size=(width,height)
videoWriter = cv2.VideoWriter(video_file_path,fourcc=fourcc,fps=fps,frameSize=size)
for i in range(len(os.listdir(img_root))):
frame = cv2.imread(img_root+str(i)+'.png')
videoWriter.write(frame)
videoWriter.release()
# 合并视频和音频 audio_path video_file_path
video = VideoFileClip(video_file_path)
audio_clip = AudioFileClip(audio_path)
video = video.set_audio(audio_clip)
video.write_videofile(file_path + "_hide.avi")
clean_tmp()
if __name__ == "__main__":
while True:
print("1.Hide a message in video 2.Reveal the secret from video")
print("any other value to exit")
choice = input()
if choice == '1':
main()
elif choice == '2':
decode_string(input("enter the name of video with extension: "))
else:
break
I have tried mp4, avi, wov format. But none of them worked.
IF YOU HAVE ANY IDEA OR SUGGESTION GIVEN TO ME, I WOULD BE VERY GRATEFUL

How to capture video by video from one rtsp Url using Opencv?

The server is sending video by video using the same RTSP URL(rtsp://192.168.0.2:8554/)
I can capture and display video using opencv.
import numpy as np
import cv2 as cv
os.environ["OPENCV_FFMPEG_CAPTURE_OPTIONS"] = "rtsp_transport;udp"
cap = cv.VideoCapture('rtsp://192.168.0.2:8554/')
while cap.isOpened():
ret, frame = cap.read()
# if frame is read correctly ret is True
if not ret:
print("Can't receive frame (stream end?). Exiting ...")
break
cv.imshow('frame', frame)
if cv.waitKey(1) == ord('q'):
break
cap.release()
cv.destroyAllWindows()
This program returns error when going on to the next video.
I tried this, but this didn't work.
import cv2 as cv
import os
import time
os.environ["OPENCV_FFMPEG_CAPTURE_OPTIONS"] = "rtsp_transport;udp"
cap = cv.VideoCapture('rtsp://192.168.0.26:8554/')
if not cap.isOpened():
print("Cannot open camera")
exit()
while True:
try:
time.sleep(2)
# Capture frame-by-frame
ret, frame = cap.read()
# if frame is read correctly ret is True
# Our operations on the frame come here
# Display the resulting frame
cv.imshow('frame',frame)
if cv.waitKey(1) == ord('q'):
break
except:
print("Exception!!")
# When everything done, release the capture
cap.release()
cv.destroyAllWindows()
Can I get some help?
Thanks in advance!
I solved this by using multi-threaded program.
Main file
from datasets import LoadStreams
import threading
import os
import logging
import cv2
import torch
import time
logger = logging.getLogger(__name__)
def select_device(device='', batch_size=None):
# device = 'cpu' or '0' or '0,1,2,3'
cpu_request = device.lower() == 'cpu'
if device and not cpu_request: # if device requested other than 'cpu'
os.environ['CUDA_VISIBLE_DEVICES'] = device # set environment variable
assert torch.cuda.is_available(), f'CUDA unavailable, invalid device {device} requested' # check availablity
cuda = False if cpu_request else torch.cuda.is_available()
if cuda:
c = 1024 ** 2 # bytes to MB
ng = torch.cuda.device_count()
if ng > 1 and batch_size: # check that batch_size is compatible with device_count
assert batch_size % ng == 0, f'batch-size {batch_size} not multiple of GPU count {ng}'
x = [torch.cuda.get_device_properties(i) for i in range(ng)]
s = f'Using torch {torch.__version__} '
for i, d in enumerate((device or '0').split(',')):
if i == 1:
s = ' ' * len(s)
logger.info(f"{s}CUDA:{d} ({x[i].name}, {x[i].total_memory / c}MB)")
else:
logger.info(f'Using torch {torch.__version__} CPU')
logger.info('') # skip a line
return torch.device('cuda:0' if cuda else 'cpu')
def detect(rtsp_url):
dataset = LoadStreams(rtsp_url)
device = select_device('')
count = 0
view_img = True
# img = torch.zeros((1, 3, imgsz, imgsz), device=device) # init img
try:
for frame_idx, (path, img, im0s, vid_cap) in enumerate(dataset): # for every frame
count += 1
im0 = im0s[0].copy()
if view_img:
cv2.imshow(str(path), im0)
# if cv2.waitKey(1) == ord('q'): # q to quit
# raise StopIteration
except:
print("finish execption")
dataset.stop()
return "good"
if __name__ == '__main__':
rtsp_url = "rtsp://192.168.0.26:8554/"
while True:
for thread in threading.enumerate():
print(thread.name)
print(detect(rtsp_url))
dataset class file
import glob
import logging
import math
import os
import random
import shutil
import time
import re
from itertools import repeat
from multiprocessing.pool import ThreadPool
from pathlib import Path
from threading import Thread
import cv2
import numpy as np
import torch
class LoadStreams: # multiple IP or RTSP cameras
def __init__(self, sources='streams.txt', img_size=640):
self.mode = 'stream'
self.img_size = img_size
self.capture = None
self.my_thread = None
self.stopFlag = False
if os.path.isfile(sources):
with open(sources, 'r') as f:
sources = [x.strip() for x in f.read().strip().splitlines() if len(x.strip())]
else:
sources = [sources]
n = len(sources)
self.imgs = [None] * n
self.sources = [clean_str(x) for x in sources] # clean source names for later
s = sources[0]
# for i, s in enumerate(sources):
# Start the thread to read frames from the video stream
# print('%g/%g: %s... ' % (i + 1, n, s), end='')
cap = cv2.VideoCapture(eval(s) if s.isnumeric() else s)
assert cap.isOpened(), 'Failed to open %s' % s
w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = cap.get(cv2.CAP_PROP_FPS) % 100
self.ret, self.imgs[0] = cap.read() # guarantee first frame
thread = Thread(target=self.update, args=([0, cap]), daemon=True)
print(' success (%gx%g at %.2f FPS).' % (w, h, fps))
thread.start()
self.capture = cap
self.my_thread = thread
print('') # newline
# check for common shapes
s = np.stack([letterbox(x, new_shape=self.img_size)[0].shape for x in self.imgs], 0) # inference shapes
self.rect = np.unique(s, axis=0).shape[0] == 1 # rect inference if all shapes equal
if not self.rect:
print('WARNING: Different stream shapes detected. For optimal performance supply similarly-shaped streams.')
def update(self, index, cap):
# Read next stream frame in a daemon thread
n = 0
while cap.isOpened() and not self.stopFlag:
n += 1
# _, self.imgs[index] = cap.read()
cap.grab()
if n == 4: # read every 4th frame
_, self.imgs[index] = cap.retrieve()
n = 0
time.sleep(0.01) # wait time
def stop(self):
self.stopFlag = True
try:
# self.capture.release()
# self.my_thrsead.join()
print("stop thread!!")
except:
print("ERROR stopping thread!!")
def __iter__(self):
self.count = -1
return self
def __next__(self):
self.count += 1
img0 = self.imgs.copy()
if cv2.waitKey(1) == ord('q'): # q to quit
cv2.destroyAllWindows()
raise StopIteration
if not self.ret:
print("error!!!")
self.stop()
# Letterbox
img = [letterbox(x, new_shape=self.img_size, auto=self.rect)[0] for x in img0]
# Stack
img = np.stack(img, 0)
# Convert
img = img[:, :, :, ::-1].transpose(0, 3, 1, 2) # BGR to RGB, to bsx3x416x416
img = np.ascontiguousarray(img)
return self.sources, img, img0, None
def __len__(self):
return 0 # 1E12 frames = 32 streams at 30 FPS for 30 years
# def stop(self):
def clean_str(s):
# Cleans a string by replacing special characters with underscore _
return re.sub(pattern="[|##!¡·$€%&()=?¿^*;:,¨´><+]", repl="_", string=s)
def letterbox(img, new_shape=(640, 640), color=(114, 114, 114), auto=True, scaleFill=False, scaleup=True):
# Resize image to a 32-pixel-multiple rectangle https://github.com/ultralytics/yolov3/issues/232
shape = img.shape[:2] # current shape [height, width]
if isinstance(new_shape, int):
new_shape = (new_shape, new_shape)
# Scale ratio (new / old)
r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])
if not scaleup: # only scale down, do not scale up (for better test mAP)
r = min(r, 1.0)
# Compute padding
ratio = r, r # width, height ratios
new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))
dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1] # wh padding
if auto: # minimum rectangle
dw, dh = np.mod(dw, 32), np.mod(dh, 32) # wh padding
elif scaleFill: # stretch
dw, dh = 0.0, 0.0
new_unpad = (new_shape[1], new_shape[0])
ratio = new_shape[1] / shape[1], new_shape[0] / shape[0] # width, height ratios
dw /= 2 # divide padding into 2 sides
dh /= 2
if shape[::-1] != new_unpad: # resize
img = cv2.resize(img, new_unpad, interpolation=cv2.INTER_LINEAR)
top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))
left, right = int(round(dw - 0.1)), int(round(dw + 0.1))
img = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color) # add border
return img, ratio, (dw, dh)
while cap.isOpened() and not self.stopFlag:
this line is especially important because
without this line the threads will be stacked and will have memory error
as the stack stacks up.

Issue in terminating a loop

I have the following code reading more frames than are in the folder. I am facing issue to stop when all the images are read from a folder instead of going again in the loop and reading the frames again I need to stop this cycle.
IMAGE_FOLDER="frames"
filenames = [f"{IMAGE_FOLDER}/frame{i}.jpg" for i in range(1, 2522)]
filenames = ["{}/frame{}.jpg".format(IMAGE_FOLDER,i) for i in range(1, 2522)]
SERVER_A_ADDRESS = "tcp://localhost:5555"
context = zmq.Context()
socket_server_a = context.socket(zmq.PUSH)
socket_server_a.connect(SERVER_A_ADDRESS)
destination = {
"currentSocket": socket_server_a,}
running = True
endpoint_responses = 0
frames= 0
def send_frame(frame, frame_requests):
global destination, running
try:
frame = cv2.resize(frame, (224, 224))
encoded, buffer = cv2.imencode('.jpg', frame)
jpg_as_text = base64.b64encode(buffer)
print("{} ( i ) : Sending frame {} to {}...".format(time.strftime('%H:%M:%S'),frame_requests))
destination["currentSocket"].send(jpg_as_text)
except Exception as Error:
print("{} ( ! ) : Encountered Error at frame {}\n\n> KILLING CONNECTIONS\n\nERROR MESSAGE: {Error}".format(time.strftime('%H:%M:%S'),frame_requests,Error))
running = False
def main():
global destination, running, frames
interval = 1 / 10
while running:
for img in filenames:
frames = cv2.imread(img)
frames += 1
print("{} ( i ) : frames count = {}".format(time.strftime('%H:%M:%S'),frames))
threading.Thread(target=send_frame, args=(frame, frames)).start()
time.sleep(interval)
destination["currentSocket"].close()
if __name__ == "__main__":
main()
Thanks help is highly appreciated
You are missing to assign running = Fasle one the loop is done.
Try this instead:
def main():
global destination, running, frames
interval = 1 / 10
while running:
for img in filenames:
frames = cv2.imread(img)
frames += 1
print("{} ( i ) : frames count = {}".format(time.strftime('%H:%M:%S'),frames))
threading.Thread(target=send_frame, args=(frame, frames)).start()
time.sleep(interval)
running = False
destination["currentSocket"].close()

Stop video by its play time

I created a program to extract the video into frames.I need suggestion to stop the cv2.imshow() after when there is no more frames to show.
video frames count = 88
fps = 10.0
duration= int(8)
def reading_video():
read_input = cv2.VideoCapture(r"D:\data\input.mp4")
frame_rate = int(read_input.get(cv2.CAP_PROP_FRAME_COUNT)) # 88
print("total number of frames is ", frame_rate)
fps = read_input.get(cv2.CAP_PROP_FPS) #10.0
duration = int((frame_rate/fps) %60)# 8
print(duration) # 8.0
initial_frame = 0
while read_input.isOpened():
ret, frame = read_input.read()
if ret == True:
cv2.imshow('video_frame', frame)
write_path = r"D:\data\frame_output"
write_name = 'frame0' + str(initial_frame) + '.jpg'
cv2.imwrite(os.path.join(write_path, write_name), frame)
initial_frame += 1
if cv2.waitKey(0) & 0xFF == ord('c'):
break
else:
break
instance_variable = reading_video()
instance_variable.release()
cv2.destroyAllWindows()
If waitkey(int(duration)) then it played and after no frame i do getting cv2 Assertion error.
If waitkey(0) then i do getting AttributeError: 'NoneType' object has no attribute 'release'
reading_video() doesn't uses return so it returns None and you have
instance_variable = None
and later
None.release()
Inside reading_video() you should use
return read_input
or maybe even directly
read_input.release()
without instance_variable.release()

Categories