Skip to content

Commit

Permalink
Merge pull request #47 from scramjetorg/feat/new_samples
Browse files Browse the repository at this point in the history
add fire sequence
  • Loading branch information
karoltylenda committed May 23, 2023
2 parents 07503eb + 93f0e64 commit f2f0163
Show file tree
Hide file tree
Showing 13 changed files with 110 additions and 0 deletions.
36 changes: 36 additions & 0 deletions python/seq-fire-alert/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Sequence sends forest images to the fire detecting machine learning API.
___


## Running

> ❗ Remember to [setup transform-hub locally](https://docs.scramjet.org/platform/self-hosted-installation) or use the [platform's environment](https://docs.scramjet.org/platform/quick-start) for the sequence deployment.
Open two terminals and run the following commands:


```bash
# go to 'seq-fire-alert' directory
cd python/seq-fire-alert

# build
npm run build
```

If you run this sample on Self Hosted Hub, please start it with process adapter option:

```bash
DEVELOPMENT=1 sth --runtime-adapter=process
```

Once you've built it, you need to deploy it.

```bash
si seq deploy dist
```


Now you should be able to see on the parameters on topic "fire":

```bash
si topic get pi
Binary file added python/seq-fire-alert/images/fog.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added python/seq-fire-alert/images/forest2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added python/seq-fire-alert/images/forest4.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added python/seq-fire-alert/images/forest5.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added python/seq-fire-alert/images/forest6.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added python/seq-fire-alert/images/image1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added python/seq-fire-alert/images/image2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added python/seq-fire-alert/images/image3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added python/seq-fire-alert/images/mountains.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 42 additions & 0 deletions python/seq-fire-alert/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import asyncio
from scramjet.streams import Stream
import aiohttp
import os


API_BASE = 'https://forestapi.bieda.it'
IMAGE_DIR = '/Users/spiderman/raspberry-pi-dash/seq-fire-alert/images'

provides = {
'provides': 'fire',
'contentType': 'text/plain'
}

def get_bytes(path):
with open(path, 'rb') as f:
data = f.read()
return data

async def post(data, url):
async with aiohttp.ClientSession() as session:
form_data = aiohttp.FormData()
form_data.add_field('file', data)
url = f'{API_BASE}/{url}'
async with session.post(url, data=form_data) as response:
return await response.json()

async def main(stream):
for file in os.listdir(f'{IMAGE_DIR}'):
image = get_bytes(f'{IMAGE_DIR}/{file}')
response = await post(image, 'predict')
stream.write([response])
stream.write(['some actions...'])
await asyncio.sleep(3)

async def run(context, input):
stream = Stream()
asyncio.gather(main(stream), return_exceptions=True)
return stream.map(lambda x : str(x) + "\n")



29 changes: 29 additions & 0 deletions python/seq-fire-alert/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "@scramjet/fire-alert",
"version": "0.0.1",
"main": "main.py",
"author": "mcdominik",
"license": "GPL-3.0",
"description": "raspberry pi internal parameters fetcher example",
"keywords": [
"fire",
"python",
"machine learning",
"topics"
],
"repository": {
"type": "git",
"url": "https://github.com/scramjetorg/platform-samples"
},
"engines": {
"python3": "3.8.0"
},
"scripts": {
"build": "mkdir -p dist/__pypackages__/ && pip3 install -t dist/__pypackages__/ -r requirements.txt && cp main.py package.json ./dist/",
"pack" : "si seq pack dist",
"clean": "rm -rf ./dist"
},
"dependencies": {
"@scramjet/api-client": "0.32.1"
}
}
3 changes: 3 additions & 0 deletions python/seq-fire-alert/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
scramjet-framework-py==0.10
pyee==9.0.4
aiohttp==3.8.4

0 comments on commit f2f0163

Please sign in to comment.