|
5 | 5 | from PIL import Image, ImageDraw
|
6 | 6 |
|
7 | 7 | class CrowdCounting:
|
8 |
| - def __init__(self): |
9 |
| - self.numZone = 8 |
10 |
| - self.mask=[0]*self.numZone |
11 |
| - self.crowd_count=[0]*self.numZone |
12 |
| - self.polygon=[0]*self.numZone |
13 |
| - |
| 8 | + def __init__(self,zone=0,width=1024,height=768, polygon=[865,210,933,210,933,227,968,227,968,560,934,560,934,568,865,568,865,210]): |
| 9 | + print("===========CrowdCounting:__init__:zone,width,height,polygon=", zone, width, height, polygon, "================") |
| 10 | + |
14 | 11 | #self._sensor = sensor
|
15 |
| - self.polygon[0] = [865,210,933,210,933,227,968,227,968,560,934,560,934,568,865,568,865,210] |
16 |
| - self.polygon[1] = [830,49,861,49,893,56,922,71,946,93,960,122,967,151,967,228,934,228,934,211,899,211,867,209,864,183,854,165,836,149,814,144,759,144,759,114,795,114,795,84,830,83,830,49] |
17 |
| - self.polygon[2] = [259,49,259,82,277,82,277,114,323,114,323,146,760,146,760,114,796,114,796,82,832,82,831,49,259,49] |
18 |
| - self.polygon[3] = [259,49,259,82,277,82,277,114,322,114,322,144,269,144,246,146,226,156,212,173,204,190,204,212,174,212,172,214,143,214,143,161,157,127,182,103,214,87,231,83,230,49,259,49] |
19 |
| - self.polygon[4] = [140,571,174,571,206,563,206,211,140,211,140,571] |
20 |
| - self.polygon[5] = [206,563,174,569,142,569,142,599,158,630,182,654,212,668,242,673,298,672,298,644,326,644,326,612,271,612,248,609,227,600,215,583,206,563] |
21 |
| - self.polygon[6] = [762,611,762,642,788,642,788,672,811,672,811,704,261,704,261,672,298,672,298,642,325,642,325,611,762,611] |
22 |
| - self.polygon[7] = [966,561,966,586,964,615,954,646,933,676,900,695,866,702,810,702,788,674,788,644,762,644,762,611,817,611,840,604,857,587,868,566,896,574,901,567,933,567,933,561,966,561] |
| 12 | + # self.polygons=[0]*8 |
| 13 | + # self.polygons[0] = [865,210,933,210,933,227,968,227,968,560,934,560,934,568,865,568,865,210] |
| 14 | + # self.polygons[1] = [830,49,861,49,893,56,922,71,946,93,960,122,967,151,967,228,934,228,934,211,899,211,867,209,864,183,854,165,836,149,814,144,759,144,759,114,795,114,795,84,830,83,830,49] |
| 15 | + # self.polygons[2] = [259,49,259,82,277,82,277,114,323,114,323,146,760,146,760,114,796,114,796,82,832,82,831,49,259,49] |
| 16 | + # self.polygons[3] = [259,49,259,82,277,82,277,114,322,114,322,144,269,144,246,146,226,156,212,173,204,190,204,212,174,212,172,214,143,214,143,161,157,127,182,103,214,87,231,83,230,49,259,49] |
| 17 | + # self.polygons[4] = [140,571,174,571,206,563,206,211,140,211,140,571] |
| 18 | + # self.polygons[5] = [206,563,174,569,142,569,142,599,158,630,182,654,212,668,242,673,298,672,298,644,326,644,326,612,271,612,248,609,227,600,215,583,206,563] |
| 19 | + # self.polygons[6] = [762,611,762,642,788,642,788,672,811,672,811,704,261,704,261,672,298,672,298,642,325,642,325,611,762,611] |
| 20 | + # self.polygons[7] = [966,561,966,586,964,615,954,646,933,676,900,695,866,702,810,702,788,674,788,644,762,644,762,611,817,611,840,604,857,587,868,566,896,574,901,567,933,567,933,561,966,561] |
23 | 21 |
|
| 22 | + self.zone = zone |
| 23 | + self.mask=[0] |
| 24 | + self.crowd_count=0 |
| 25 | + self.polygon=polygon |
| 26 | + |
24 | 27 | #no matter what resolution the input video is (currently 720x1280),
|
25 | 28 | #it will resize to 1024x768 before sending to model
|
26 | 29 | #AI data input is 1/8 of image resolution, 768x1024 image, data is 1x96x128x1
|
27 |
| - self.width = 1024>>3 |
28 |
| - self.height = 768>>3 |
29 |
| - for zone in range(self.numZone): |
30 |
| - for t in range(len(self.polygon[zone])): |
31 |
| - self.polygon[zone][t] = self.polygon[zone][t]>>3 |
| 30 | + self.width = width>>3 |
| 31 | + self.height = height>>3 |
| 32 | + for t in range(len(self.polygon)): |
| 33 | + self.polygon[t] = self.polygon[t]>>3 |
32 | 34 |
|
33 | 35 | #convert polygon to mask algorithm
|
34 | 36 | #https://stackoverflow.com/questions/3654289/scipy-create-2d-polygon-mask
|
35 | 37 | self.img = Image.new('L', (self.width, self.height), 0)
|
36 |
| - for zone in range(self.numZone): |
37 |
| - self.img = Image.new('L', (self.width, self.height), 0) |
38 |
| - ImageDraw.Draw(self.img).polygon(self.polygon[zone], outline=1, fill=1) |
39 |
| - self.mask[zone] = numpy.array(self.img).flatten() |
| 38 | + ImageDraw.Draw(self.img).polygon(self.polygon, outline=1, fill=1) |
| 39 | + self.mask = numpy.array(self.img).flatten() |
40 | 40 |
|
41 | 41 | def process_frame(self, frame):
|
42 | 42 | for tensor in frame.tensors():
|
43 | 43 | data = tensor.data()
|
44 | 44 | imgData = []
|
45 | 45 | imgData.append(tensor.data())
|
46 |
| - for zone in range(self.numZone): |
47 |
| - self.crowd_count[zone] = numpy.sum(self.mask[zone] * imgData) |
| 46 | + self.crowd_count = numpy.sum(self.mask * imgData) |
48 | 47 |
|
49 | 48 | if (self.crowd_count):
|
50 | 49 | messages = list(frame.messages())
|
51 | 50 | if len(messages) > 0:
|
52 | 51 | json_msg = json.loads(messages[0].get_message())
|
53 |
| - json_msg["count"] = { |
54 |
| - "zone0":int(self.crowd_count[0]), |
55 |
| - "zone1":int(self.crowd_count[1]), |
56 |
| - "zone2":int(self.crowd_count[2]), |
57 |
| - "zone3":int(self.crowd_count[3]), |
58 |
| - "zone4":int(self.crowd_count[4]), |
59 |
| - "zone5":int(self.crowd_count[5]), |
60 |
| - "zone6":int(self.crowd_count[6]), |
61 |
| - "zone7":int(self.crowd_count[7]) |
62 |
| - } |
| 52 | + json_msg["count"] = {"zone"+str(self.zone):int(self.crowd_count)} |
63 | 53 | messages[0].set_message(json.dumps(json_msg))
|
64 |
| - else: |
65 |
| - print("No JSON messages in frame") |
66 | 54 |
|
67 | 55 | return True
|
0 commit comments