How to uplevel your Security Cam by detecting Objects with a Streamlit Data App?

Why does plain motion detection fail?

How to detect objects by using OpenCV and AI?

Download and use a pre trained model

config_file = ‘model_config.pbtxt’frozen_model = ‘graph.pb’model = cv2.dnn_DetectionModel(frozen_model, config_file)
model.setInputSize(320, 320)model.setInputScale(1.0/127.5)model.setInputMean((127.5,127.5,127.5))model.setInputSwapRB(True)model.setInputCrop(False)
ClassIndex, confidence, bbox = model.detect(img, confThreshold=0.2)

Wrap everything into a Streamlit Data App

st.title(‘Camera Object Detection’)
url = st.text_input(‘Specify a Picture URL’)if url is not None and url.startswith(‘http’):img = download_cam_image(url)img_file_buffer = NoneNow, alternatively offer an image upload widget for users to directly upload their images:img_file_buffer = st.file_uploader(“Upload an image”, type=[“png”, “jpg”, “jpeg”])if img_file_buffer is not None:image = Image.open(img_file_buffer)img_array = np.asarray(image)img = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR)
confidence_threshold = st.slider(“Confidence threshold”, 0.0, 1.0, DEFAULT_CONFIDENCE_THRESHOLD, 0.05)
st.image(img)df = pd.DataFrame(results, columns=([‘Class’, ‘Label’, ‘Confidence’]))st.dataframe(df)

Build and deploy with Docker

FROM python:3.7LABEL maintainer=”Wolfgang Beer @wolfgangB33r”EXPOSE 8501WORKDIR /appCOPY requirements.txt ./requirements.txtRUN pip3 install -r requirements.txtCOPY . .CMD streamlit run src/app.pystreamlitopencv-python-headlessnumpyrequests
docker build -f Dockerfile -t cam:latest .
docker run -p 8501:8501 cam:latest

Try it out

Get the source code

Summary

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store