mirror of https://github.com/Askill/r_place.git
drawing ref image
This commit is contained in:
parent
21e3de27df
commit
d7a1421236
|
|
@ -44,13 +44,11 @@ func get(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
defer c.Close()
|
defer c.Close()
|
||||||
ticker := time.NewTicker(1 * time.Second)
|
ticker := time.NewTicker(1 * time.Second)
|
||||||
|
|
||||||
var refImage = GetImage(img.width, img.height)
|
|
||||||
var tmpImage = GetImage(img.width, img.height)
|
var tmpImage = GetImage(img.width, img.height)
|
||||||
|
|
||||||
for range ticker.C {
|
for range ticker.C {
|
||||||
copy(refImage.pixels, img.pixels)
|
|
||||||
diff := tmpImage.GetDiff(&refImage)
|
diff := tmpImage.GetDiff(&img)
|
||||||
for i := 0; i < int(diff.width*diff.height); i++ {
|
for i := 0; i < int(diff.width*diff.height); i++ {
|
||||||
pix := diff.pixels[i]
|
pix := diff.pixels[i]
|
||||||
if pix.pixel.UserID != 0 {
|
if pix.pixel.UserID != 0 {
|
||||||
|
|
@ -70,7 +68,7 @@ func get(w http.ResponseWriter, r *http.Request) {
|
||||||
if err := c.WriteMessage(websocket.PingMessage, []byte{}); err != nil {
|
if err := c.WriteMessage(websocket.PingMessage, []byte{}); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
copy(tmpImage.pixels, refImage.pixels)
|
copy(tmpImage.pixels, img.pixels)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -53,7 +53,7 @@ func (img *image) SetPixel(message Message) int {
|
||||||
fmt.Printf("User %d tried accessing out of bounds \n", message.UserID)
|
fmt.Printf("User %d tried accessing out of bounds \n", message.UserID)
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
if message.Color >= 16 || message.Color < 0 {
|
if message.Color >= 255 || message.Color < 0 {
|
||||||
fmt.Printf("User %d tried setting non existent color \n", message.UserID)
|
fmt.Printf("User %d tried setting non existent color \n", message.UserID)
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,8 @@ import numpy as np
|
||||||
|
|
||||||
import websockets
|
import websockets
|
||||||
import cv2
|
import cv2
|
||||||
|
import matplotlib.image as mpimg
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class pixel:
|
class pixel:
|
||||||
|
|
@ -22,13 +24,15 @@ class pixel:
|
||||||
timestamp: int
|
timestamp: int
|
||||||
userid: int
|
userid: int
|
||||||
|
|
||||||
async def sender():
|
async def sender(img):
|
||||||
async with websockets.connect("ws://localhost:8080/set") as websocket:
|
async with websockets.connect("ws://localhost:8080/set") as websocket:
|
||||||
while True:
|
while True:
|
||||||
|
rx = random.randint(0, 999)
|
||||||
|
ry = random.randint(0, 999)
|
||||||
message = pixel(
|
message = pixel(
|
||||||
x=random.randint(0, 999),
|
x=rx,
|
||||||
y=random.randint(0, 999),
|
y=ry,
|
||||||
color=random.randint(0,15),
|
color=int(img[rx][ry][0]*255),
|
||||||
timestamp=int(time.time()),
|
timestamp=int(time.time()),
|
||||||
userid=1,
|
userid=1,
|
||||||
)
|
)
|
||||||
|
|
@ -37,7 +41,7 @@ async def sender():
|
||||||
if succ == "1":
|
if succ == "1":
|
||||||
print(message, "was not set")
|
print(message, "was not set")
|
||||||
|
|
||||||
await asyncio.sleep(0.1)
|
await asyncio.sleep(0.01)
|
||||||
|
|
||||||
async def client():
|
async def client():
|
||||||
image = np.zeros(shape=[1000, 1000, 3], dtype=np.uint8)
|
image = np.zeros(shape=[1000, 1000, 3], dtype=np.uint8)
|
||||||
|
|
@ -50,15 +54,18 @@ async def client():
|
||||||
while True:
|
while True:
|
||||||
i+=1
|
i+=1
|
||||||
x = pixel(**json.loads(await websocket.recv()))
|
x = pixel(**json.loads(await websocket.recv()))
|
||||||
image[x.x][x.y] = ([y*255 for y in colors[x.color]])
|
#image[x.x][x.y] = ([y*255 for y in colors[x.color]])
|
||||||
#if i% 4000 == 0:
|
image[x.x][x.y] = ((x.color, x.color, x.color))
|
||||||
# cv2.imshow("changes x", image)
|
if i% 1000 == 0:
|
||||||
# cv2.waitKey(10) & 0XFF
|
print("showing")
|
||||||
|
cv2.imshow("changes x", image)
|
||||||
|
cv2.waitKey(10) & 0XFF
|
||||||
await websocket.send("1")
|
await websocket.send("1")
|
||||||
#print(i, x)
|
#print(i, x)
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
coros = [sender() for _ in range(500)]
|
img=mpimg.imread('logo.png')
|
||||||
|
coros = [sender(img) for _ in range(10)]
|
||||||
coros.append(client())
|
coros.append(client())
|
||||||
returns = await asyncio.gather(*coros)
|
returns = await asyncio.gather(*coros)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue