Spaces:
Sleeping
Sleeping
import streamlit as st | |
import cv2 | |
from PIL import Image | |
import numpy as np | |
from imutils.video import VideoStream | |
import pypdf | |
import pikepdf | |
import pdfminer.high_level | |
import fpdf | |
import pdfkit | |
import pdfplumber | |
import fitz # pymupdf | |
import borb.pdf | |
import pdf2image | |
import vidgear.gears | |
import av | |
# ๐๐ฅ Set up the Streamlit UI with an impressive demo style! | |
st.title("๐ Impressive Library Demo Extravaganza") | |
st.write("Watch these libraries flex their muscles with live demos producing real outputs!") | |
# ๐ Demo Functions | |
def demo_pypdf(): | |
with open("demo.pdf", "wb") as f: | |
f.write(b"Hello from pypdf!") | |
reader = pypdf.PdfReader("demo.pdf") | |
return reader.pages[0].extract_text() if reader.pages else "No text found!" | |
def demo_pikepdf(): | |
pdf = pikepdf.Pdf.new() | |
pdf.pages.append(pikepdf.Page(pikepdf.Dictionary())) | |
pdf.save("demo_pikepdf.pdf") | |
return "demo_pikepdf.pdf" | |
def demo_pdfminer(): | |
with open("demo.pdf", "wb") as f: | |
f.write(b"PDFminer magic!") | |
text = pdfminer.high_level.extract_text("demo.pdf") | |
return text | |
def demo_fpdf(): | |
pdf = fpdf.FPDF() | |
pdf.add_page() | |
pdf.set_font("Arial", size=12) | |
pdf.cell(200, 10, txt="FPDF rocks!", ln=True) | |
pdf.output("demo_fpdf.pdf") | |
return "demo_fpdf.pdf" | |
def demo_pdfkit(): | |
pdfkit.from_string("<h1>Hello PDFkit!</h1>", "demo_pdfkit.pdf") | |
return "demo_pdfkit.pdf" | |
def demo_pdfplumber(): | |
with open("demo.pdf", "wb") as f: | |
f.write(b"Plumb this text!") | |
with pdfplumber.open("demo.pdf") as pdf: | |
return pdf.pages[0].extract_text() if pdf.pages else "No text!" | |
def demo_pymupdf(): | |
doc = fitz.open() | |
page = doc.new_page() | |
page.insert_text((100, 100), "Pymupdf shines!") | |
doc.save("demo_pymupdf.pdf") | |
return "demo_pymupdf.pdf" | |
def demo_borb(): | |
doc = borb.pdf.Document() | |
page = borb.pdf.Page() | |
doc.append_page(page) | |
doc.save("demo_borb.pdf") | |
return "demo_borb.pdf" | |
def demo_pdf2image(): | |
with open("demo.pdf", "wb") as f: | |
f.write(b"Image this!") | |
images = pdf2image.convert_from_path("demo.pdf") | |
images[0].save("demo_pdf2image.jpg") | |
return "demo_pdf2image.jpg" | |
def demo_opencv_image(): | |
cap = cv2.VideoCapture(0) | |
ret, frame = cap.read() | |
if ret: | |
cv2.imwrite("demo_opencv.jpg", frame) | |
cap.release() | |
return "demo_opencv.jpg" | |
cap.release() | |
return None | |
def demo_imutils(): | |
vs = VideoStream(src=0).start() | |
frame = vs.read() | |
vs.stop() | |
if frame is not None: | |
cv2.imwrite("demo_imutils.jpg", cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)) | |
return "demo_imutils.jpg" | |
return None | |
def demo_vidgear(): | |
stream = vidgear.gears.CamGear(source=0).start() | |
frame = stream.read() | |
stream.stop() | |
if frame is not None: | |
cv2.imwrite("demo_vidgear.jpg", frame) | |
return "demo_vidgear.jpg" | |
return None | |
# ๐ Main Demo Interface | |
st.sidebar.header("๐ฎ Demo Controls") | |
demo_type = st.sidebar.selectbox("Choose a Demo Category", ["PDF", "Image Capture", "Video Capture"]) | |
# ๐ PDF Demos | |
if demo_type == "PDF": | |
st.subheader("๐ PDF Powerhouse") | |
demo = st.selectbox("Pick a PDF Demo", [ | |
"pypdf", "pikepdf", "pdfminer.six", "fpdf2", "pdfkit", | |
"pdfplumber", "pymupdf", "borb", "pdf2image" | |
]) | |
if st.button("Run PDF Demo"): | |
with st.spinner("Running demo..."): | |
try: | |
if demo == "pypdf": | |
result = demo_pypdf() | |
st.write("Extracted Text:", result) | |
elif demo == "pikepdf": | |
result = demo_pikepdf() | |
st.download_button("Download PDF", open(result, "rb").read(), file_name=result) | |
elif demo == "pdfminer.six": | |
result = demo_pdfminer() | |
st.write("Extracted Text:", result) | |
elif demo == "fpdf2": | |
result = demo_fpdf() | |
st.download_button("Download PDF", open(result, "rb").read(), file_name=result) | |
elif demo == "pdfkit": | |
result = demo_pdfkit() | |
st.download_button("Download PDF", open(result, "rb").read(), file_name=result) | |
elif demo == "pdfplumber": | |
result = demo_pdfplumber() | |
st.write("Extracted Text:", result) | |
elif demo == "pymupdf": | |
result = demo_pymupdf() | |
st.download_button("Download PDF", open(result, "rb").read(), file_name=result) | |
elif demo == "borb": | |
result = demo_borb() | |
st.download_button("Download PDF", open(result, "rb").read(), file_name=result) | |
elif demo == "pdf2image": | |
result = demo_pdf2image() | |
st.image(result, caption="Converted PDF to Image") | |
except Exception as e: | |
st.error(f"Demo failed: {str(e)}") | |
# ๐ธ Image Capture Demos | |
elif demo_type == "Image Capture": | |
st.subheader("๐ธ Image Snappers") | |
demo = st.selectbox("Pick an Image Demo", ["opencv-python", "imutils"]) | |
if st.button("Run Image Demo"): | |
with st.spinner("Capturing image..."): | |
try: | |
if demo == "opencv-python": | |
result = demo_opencv_image() | |
if result: | |
st.image(result, caption="Snapped with OpenCV!") | |
else: | |
st.error("Camera not detected!") | |
elif demo == "imutils": | |
result = demo_imutils() | |
if result: | |
st.image(result, caption="Snapped with Imutils!") | |
else: | |
st.error("Camera not detected!") | |
except Exception as e: | |
st.error(f"Demo failed: {str(e)}") | |
# ๐ฅ Video Capture Demos | |
elif demo_type == "Video Capture": | |
st.subheader("๐ฅ Video Vanguards") | |
demo = st.selectbox("Pick a Video Demo", ["opencv-python", "vidgear"]) | |
if st.button("Run Video Demo"): | |
with st.spinner("Capturing video frame..."): | |
try: | |
if demo == "opencv-python": | |
result = demo_opencv_image() # Using image capture as a proxy | |
if result: | |
st.image(result, caption="Frame from OpenCV Video!") | |
else: | |
st.error("Camera not detected!") | |
elif demo == "vidgear": | |
result = demo_vidgear() | |
if result: | |
st.image(result, caption="Frame from VidGear Video!") | |
else: | |
st.error("Camera not detected!") | |
except Exception as e: | |
st.error(f"Demo failed: {str(e)}") | |
# ๐ Results Display | |
st.write("### Results") | |
st.write("Check out your impressive outputs above!") |