Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[flake8]
max-line-length = 100
extend-ignore = E203, W503, E501
exclude =
.git,
__pycache__,
.venv,
venv,
.eggs,
*.egg,
build,
dist,
.mypy_cache,
.pytest_cache,
.coverage,
htmlcov
per-file-ignores =
__init__.py:F401
count = True
statistics = True
62 changes: 62 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
name: Python Linting and Quality Checks

on:
push:
branches: [main, develop]
pull_request:
branches: [main, develop]

jobs:
lint:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.11', '3.12']

steps:
- uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
cache: 'pip'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements-dev.txt

- name: Check code formatting with Black
run: black --check .
continue-on-error: true

- name: Check import sorting with isort
run: isort --check-only .
continue-on-error: true

- name: Lint with flake8
run: flake8 .
continue-on-error: true

- name: Lint with pylint
run: |
find . -type f -name "*.py" -not -path "./.*" -not -path "./.venv/*" | xargs pylint --exit-zero --disable=fixme
continue-on-error: true

- name: Type check with mypy
run: mypy .
continue-on-error: true

- name: Run tests with pytest
run: pytest --cov=. --cov-report=xml --cov-report=term-missing
continue-on-error: true

- name: Upload coverage reports
if: matrix.python-version == '3.11'
uses: codecov/codecov-action@v3
with:
files: ./coverage.xml
flags: unittests
name: codecov-umbrella
fail_ci_if_error: false
64 changes: 64 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,66 @@
# macOS
.DS_Store
# Python
__pycache__/
*.py[cod]
*$py.class
*.so
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# Virtual environments
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Testing and coverage
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
htmlcov/

# Type checking
.mypy_cache/
.dmypy.json
dmypy.json

# IDEs
.vscode/
.idea/
*.swp
*.swo
*~
.project
.pydevproject

# Environment variables
.env
.env.local
67 changes: 35 additions & 32 deletions AI Computer Vision with MoonDream/lab-bottle-boundingbox.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
from bottle import run, route, post, request, static_file
import base64
import requests
import os

import requests
from bottle import post, request, route, run, static_file

folder = "pic"
files = os.listdir(folder)


def ai(image, query):
with open(image, "rb") as f:
img_b64 = base64.b64encode(f.read()).decode()

payload = {
"image_url": f"data:image/png;base64,{img_b64}",
"object": query
}

payload = {"image_url": f"data:image/png;base64,{img_b64}", "object": query}

response = requests.post("http://localhost:2021/v1/detect", json=payload).json()
return response['objects']
return response["objects"]


def gallery(coordinates, image):
picture = f'''
picture = f"""
<div style="position:relative;
display: inline-block;
border: 2px solid black;
height: 200px;
width: auto;">
<img style="height:100%; width:auto;" src="{image}">
'''
"""
for image in coordinates:
x = image['x_min'] * 100
width = (image['x_max'] - image['x_min']) * 100
y = image['y_min'] * 100
height = (image['y_max'] - image['y_min']) * 100
x = image["x_min"] * 100
width = (image["x_max"] - image["x_min"]) * 100
y = image["y_min"] * 100
height = (image["y_max"] - image["y_min"]) * 100

picture += f'''
picture += f"""
<div style="position: absolute;
border: 2px solid lime;
box-sizing: border-box;
Expand All @@ -42,45 +42,48 @@ def gallery(coordinates, image):
width:{width}%;
height:{height}%;">
</div>
'''
picture += '</div>'
"""
picture += "</div>"

return picture

@route('/', method=['GET','POST'])

@route("/", method=["GET", "POST"])
def index():
query = request.forms.get('find')
form = '''
query = request.forms.get("find")
form = """
<form method="post" action="/">
Find: <input type="text" name="find">
<br>
<input type="submit">
</form>
'''
gallery_html=''
"""
gallery_html = ""
if query != None:
for x in files:
image = os.path.join('pic', x)
image = os.path.join("pic", x)
coordinates = ai(image, query)
picture = gallery(coordinates, image)
gallery_html+=picture
gallery_html += picture

page = f'''
page = f"""
<h1>Web App</h1>
{form}
<br>
{gallery_html}
'''
"""
else:
page = f'''
page = f"""
<h1>Web App</h1>
<br>
{form}
'''
"""
return page

@route('/<filename:path>')

@route("/<filename:path>")
def serve_static(filename):
return static_file(filename, root='./')
return static_file(filename, root="./")


run(host='127.0.0.1', port=8080)
run(host="127.0.0.1", port=8080)
39 changes: 21 additions & 18 deletions AI Computer Vision with MoonDream/lab-security.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,53 @@
import cv2 as cv
import time
import base64
import requests
import os
import time

import cv2 as cv
import requests


def camera():
cam = cv.VideoCapture(1)
time.sleep(0.5)
ret, frame = cam.read()
image = 'captured_image.png'
image = "captured_image.png"

if ret:
cv.imwrite(image, frame)
cv.imwrite(image, frame)
else:
print("Failed to capture image.")

cam.release()
return image


def ai(image):
with open(image, "rb") as f:
img_b64 = base64.b64encode(f.read()).decode()

payload = {
"image_url": f"data:image/png;base64,{img_b64}",
"question":
"""
"question": """
if there a person in this image in orange clothing answer 'orange'.
If there is a person in the image without orange clothing answer 'person'.
If there is no person answer 'empty'
"""
""",
}

resp = requests.post("http://localhost:2021/v1/query", json=payload).json()
return resp['answer']
return resp["answer"]


while True:
image = camera()
response = ai(image)
print(response)
if 'person' in response:
os.system('afplay alert.mp3')
elif 'orange' in response:
os.system('afplay welcome.mp3')
if "person" in response:
os.system("afplay alert.mp3")
elif "orange" in response:
os.system("afplay welcome.mp3")
else:
os.system('afplay crickets.wav')
os.system("afplay crickets.wav")

print(response)
time.sleep(1)
time.sleep(1)
Loading