2022-05-26 12:14:36 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
|
from dataclasses import dataclass
|
|
|
|
|
import datetime
|
|
|
|
|
|
|
|
|
|
import json
|
2022-06-03 07:49:59 +00:00
|
|
|
from multiprocessing import Pool
|
2022-05-27 22:30:15 +00:00
|
|
|
import random
|
2022-05-26 12:14:36 +00:00
|
|
|
import time
|
2022-05-29 10:08:56 +00:00
|
|
|
import matplotlib
|
|
|
|
|
from matplotlib import pyplot as plt
|
|
|
|
|
import numpy as np
|
2022-05-26 12:14:36 +00:00
|
|
|
|
|
|
|
|
import websockets
|
2022-05-29 10:08:56 +00:00
|
|
|
import cv2
|
2022-05-31 16:29:44 +00:00
|
|
|
import matplotlib.image as mpimg
|
|
|
|
|
|
2022-05-26 12:14:36 +00:00
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
|
class pixel:
|
|
|
|
|
x: int
|
|
|
|
|
y: int
|
|
|
|
|
color: int
|
|
|
|
|
timestamp: int
|
|
|
|
|
userid: int
|
|
|
|
|
|
2022-05-31 16:29:44 +00:00
|
|
|
async def sender(img):
|
2022-05-29 09:34:43 +00:00
|
|
|
async with websockets.connect("ws://localhost:8080/set") as websocket:
|
2022-05-28 14:12:56 +00:00
|
|
|
while True:
|
2022-05-31 16:29:44 +00:00
|
|
|
rx = random.randint(0, 999)
|
|
|
|
|
ry = random.randint(0, 999)
|
2022-05-27 22:30:15 +00:00
|
|
|
message = pixel(
|
2022-05-31 16:29:44 +00:00
|
|
|
x=rx,
|
|
|
|
|
y=ry,
|
2022-06-05 13:44:28 +00:00
|
|
|
color=int(sum(img[rx][ry])/3),
|
2022-05-27 22:30:15 +00:00
|
|
|
timestamp=int(time.time()),
|
|
|
|
|
userid=1,
|
|
|
|
|
)
|
|
|
|
|
await websocket.send(json.dumps(message.__dict__))
|
2022-05-31 14:15:09 +00:00
|
|
|
succ = await websocket.recv()
|
|
|
|
|
if succ == "1":
|
|
|
|
|
print(message, "was not set")
|
2022-05-29 09:34:43 +00:00
|
|
|
|
2022-06-05 13:44:28 +00:00
|
|
|
|
2022-05-28 14:12:56 +00:00
|
|
|
|
|
|
|
|
async def client():
|
2022-05-31 14:15:09 +00:00
|
|
|
image = np.zeros(shape=[1000, 1000, 3], dtype=np.uint8)
|
2022-05-29 10:08:56 +00:00
|
|
|
colors = []
|
|
|
|
|
for name, hex in matplotlib.colors.cnames.items():
|
|
|
|
|
colors.append(matplotlib.colors.to_rgb(hex))
|
|
|
|
|
|
2022-05-29 09:34:43 +00:00
|
|
|
async with websockets.connect("ws://localhost:8080/get") as websocket:
|
2022-05-28 14:12:56 +00:00
|
|
|
i= 0
|
2022-05-27 22:30:15 +00:00
|
|
|
while True:
|
2022-05-28 14:12:56 +00:00
|
|
|
i+=1
|
2022-05-29 10:08:56 +00:00
|
|
|
x = pixel(**json.loads(await websocket.recv()))
|
2022-05-31 16:29:44 +00:00
|
|
|
#image[x.x][x.y] = ([y*255 for y in colors[x.color]])
|
|
|
|
|
image[x.x][x.y] = ((x.color, x.color, x.color))
|
2022-06-05 13:44:28 +00:00
|
|
|
#if i% 5000 == 0:
|
|
|
|
|
# cv2.imshow("changes x", image)
|
|
|
|
|
# cv2.waitKey(10) & 0XFF
|
2022-05-31 14:15:09 +00:00
|
|
|
await websocket.send("1")
|
|
|
|
|
#print(i, x)
|
2022-05-26 12:14:36 +00:00
|
|
|
|
2022-05-28 14:12:56 +00:00
|
|
|
async def main():
|
2022-06-05 13:44:28 +00:00
|
|
|
img=mpimg.imread('./1.jpg')
|
2022-06-03 07:49:59 +00:00
|
|
|
coros = [sender(img) for _ in range(100)]
|
2022-05-28 14:12:56 +00:00
|
|
|
coros.append(client())
|
|
|
|
|
returns = await asyncio.gather(*coros)
|
2022-05-29 09:34:43 +00:00
|
|
|
|
2022-06-04 21:25:25 +00:00
|
|
|
def asyncMain(x):
|
2022-06-03 07:49:59 +00:00
|
|
|
asyncio.get_event_loop().run_until_complete(main())
|
2022-05-26 12:14:36 +00:00
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2022-06-05 13:44:28 +00:00
|
|
|
with Pool(12) as p:
|
|
|
|
|
print(p.map(asyncMain, [() for _ in range(12)]))
|
|
|
|
|
#asyncMain(0)
|