Commit c5741d91 authored by Nguyeng Hoang Giang's avatar Nguyeng Hoang Giang

me

parent 1b705087
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
......@@ -7,6 +7,11 @@ mongo.port=27018
mongo.user=aiserveradmin
mongo.pass=AiServer123aA
mongo.database=aiserverdb
mongo.collection=SCRFD
mongo.box_collection=RAPID
mongo.background_collection=heatmap_background
mongo.track_collection=segment
model.type=scrfd
multiplier=0.7
cephAccessKey=W3YMZV41WT1W1RZYUGP0
cephPrivateKey=1fAiApgCKCkuNEsXNamRQ82TBA3aqFmeLyCDm1iS
cephHostname=http://192.168.0.128:8080
\ No newline at end of file
......@@ -60,6 +60,10 @@ dependencies {
implementation(group: 'org.slf4j', name: 'slf4j-log4j12', version:'2.0.0-alpha4')
implementation 'org.apache.cassandra:cassandra-all:0.8.1'
//s3 aws
implementation platform('com.amazonaws:aws-java-sdk-bom:1.11.1000')
implementation 'com.amazonaws:aws-java-sdk-s3'
compileOnly 'org.apache.tomcat:annotations-api:6.0.53'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
......
......@@ -14,95 +14,55 @@ public final class TimeRequest extends
private TimeRequest() {
}
public static final int START_FIELD_NUMBER = 1;
private com.proto.common.Time start_;
private long start_;
/**
* <code>.common.Time start = 1;</code>
* <code>int64 start = 1;</code>
* @return The start.
*/
@java.lang.Override
public boolean hasStart() {
return start_ != null;
public long getStart() {
return start_;
}
/**
* <code>.common.Time start = 1;</code>
* <code>int64 start = 1;</code>
* @param value The start to set.
*/
@java.lang.Override
public com.proto.common.Time getStart() {
return start_ == null ? com.proto.common.Time.getDefaultInstance() : start_;
}
/**
* <code>.common.Time start = 1;</code>
*/
private void setStart(com.proto.common.Time value) {
value.getClass();
start_ = value;
}
/**
* <code>.common.Time start = 1;</code>
*/
@java.lang.SuppressWarnings({"ReferenceEquality"})
private void mergeStart(com.proto.common.Time value) {
value.getClass();
if (start_ != null &&
start_ != com.proto.common.Time.getDefaultInstance()) {
start_ =
com.proto.common.Time.newBuilder(start_).mergeFrom(value).buildPartial();
} else {
start_ = value;
}
private void setStart(long value) {
start_ = value;
}
/**
* <code>.common.Time start = 1;</code>
* <code>int64 start = 1;</code>
*/
private void clearStart() { start_ = null;
private void clearStart() {
start_ = 0L;
}
public static final int END_FIELD_NUMBER = 2;
private com.proto.common.Time end_;
/**
* <code>.common.Time end = 2;</code>
*/
@java.lang.Override
public boolean hasEnd() {
return end_ != null;
}
private long end_;
/**
* <code>.common.Time end = 2;</code>
* <code>int64 end = 2;</code>
* @return The end.
*/
@java.lang.Override
public com.proto.common.Time getEnd() {
return end_ == null ? com.proto.common.Time.getDefaultInstance() : end_;
public long getEnd() {
return end_;
}
/**
* <code>.common.Time end = 2;</code>
* <code>int64 end = 2;</code>
* @param value The end to set.
*/
private void setEnd(com.proto.common.Time value) {
value.getClass();
end_ = value;
}
/**
* <code>.common.Time end = 2;</code>
*/
@java.lang.SuppressWarnings({"ReferenceEquality"})
private void mergeEnd(com.proto.common.Time value) {
value.getClass();
if (end_ != null &&
end_ != com.proto.common.Time.getDefaultInstance()) {
end_ =
com.proto.common.Time.newBuilder(end_).mergeFrom(value).buildPartial();
} else {
end_ = value;
}
private void setEnd(long value) {
end_ = value;
}
/**
* <code>.common.Time end = 2;</code>
* <code>int64 end = 2;</code>
*/
private void clearEnd() { end_ = null;
private void clearEnd() {
end_ = 0L;
}
public static com.proto.common.TimeRequest parseFrom(
......@@ -201,95 +161,57 @@ public final class TimeRequest extends
/**
* <code>.common.Time start = 1;</code>
*/
@java.lang.Override
public boolean hasStart() {
return instance.hasStart();
}
/**
* <code>.common.Time start = 1;</code>
* <code>int64 start = 1;</code>
* @return The start.
*/
@java.lang.Override
public com.proto.common.Time getStart() {
public long getStart() {
return instance.getStart();
}
/**
* <code>.common.Time start = 1;</code>
* <code>int64 start = 1;</code>
* @param value The start to set.
* @return This builder for chaining.
*/
public Builder setStart(com.proto.common.Time value) {
public Builder setStart(long value) {
copyOnWrite();
instance.setStart(value);
return this;
}
/**
* <code>.common.Time start = 1;</code>
*/
public Builder setStart(
com.proto.common.Time.Builder builderForValue) {
copyOnWrite();
instance.setStart(builderForValue.build());
return this;
}
/**
* <code>.common.Time start = 1;</code>
* <code>int64 start = 1;</code>
* @return This builder for chaining.
*/
public Builder mergeStart(com.proto.common.Time value) {
public Builder clearStart() {
copyOnWrite();
instance.mergeStart(value);
return this;
}
/**
* <code>.common.Time start = 1;</code>
*/
public Builder clearStart() { copyOnWrite();
instance.clearStart();
return this;
}
/**
* <code>.common.Time end = 2;</code>
*/
@java.lang.Override
public boolean hasEnd() {
return instance.hasEnd();
}
/**
* <code>.common.Time end = 2;</code>
* <code>int64 end = 2;</code>
* @return The end.
*/
@java.lang.Override
public com.proto.common.Time getEnd() {
public long getEnd() {
return instance.getEnd();
}
/**
* <code>.common.Time end = 2;</code>
* <code>int64 end = 2;</code>
* @param value The end to set.
* @return This builder for chaining.
*/
public Builder setEnd(com.proto.common.Time value) {
public Builder setEnd(long value) {
copyOnWrite();
instance.setEnd(value);
return this;
}
/**
* <code>.common.Time end = 2;</code>
*/
public Builder setEnd(
com.proto.common.Time.Builder builderForValue) {
copyOnWrite();
instance.setEnd(builderForValue.build());
return this;
}
/**
* <code>.common.Time end = 2;</code>
* <code>int64 end = 2;</code>
* @return This builder for chaining.
*/
public Builder mergeEnd(com.proto.common.Time value) {
public Builder clearEnd() {
copyOnWrite();
instance.mergeEnd(value);
return this;
}
/**
* <code>.common.Time end = 2;</code>
*/
public Builder clearEnd() { copyOnWrite();
instance.clearEnd();
return this;
}
......@@ -314,7 +236,8 @@ public final class TimeRequest extends
"end_",
};
java.lang.String info =
"\u0000\u0002\u0000\u0000\u0001\u0002\u0002\u0000\u0000\u0000\u0001\t\u0002\t";
"\u0000\u0002\u0000\u0000\u0001\u0002\u0002\u0000\u0000\u0000\u0001\u0002\u0002\u0002" +
"";
return newMessageInfo(DEFAULT_INSTANCE, info, objects);
}
// fall through
......
......@@ -8,24 +8,14 @@ public interface TimeRequestOrBuilder extends
com.google.protobuf.MessageLiteOrBuilder {
/**
* <code>.common.Time start = 1;</code>
* @return Whether the start field is set.
*/
boolean hasStart();
/**
* <code>.common.Time start = 1;</code>
* <code>int64 start = 1;</code>
* @return The start.
*/
com.proto.common.Time getStart();
long getStart();
/**
* <code>.common.Time end = 2;</code>
* @return Whether the end field is set.
*/
boolean hasEnd();
/**
* <code>.common.Time end = 2;</code>
* <code>int64 end = 2;</code>
* @return The end.
*/
com.proto.common.Time getEnd();
long getEnd();
}
......@@ -133,58 +133,6 @@ public final class ServerRequest extends
}
public static final int WIDTH_FIELD_NUMBER = 4;
private int width_;
/**
* <code>int32 width = 4;</code>
* @return The width.
*/
@java.lang.Override
public int getWidth() {
return width_;
}
/**
* <code>int32 width = 4;</code>
* @param value The width to set.
*/
private void setWidth(int value) {
width_ = value;
}
/**
* <code>int32 width = 4;</code>
*/
private void clearWidth() {
width_ = 0;
}
public static final int HEIGHT_FIELD_NUMBER = 5;
private int height_;
/**
* <code>int32 height = 5;</code>
* @return The height.
*/
@java.lang.Override
public int getHeight() {
return height_;
}
/**
* <code>int32 height = 5;</code>
* @param value The height to set.
*/
private void setHeight(int value) {
height_ = value;
}
/**
* <code>int32 height = 5;</code>
*/
private void clearHeight() {
height_ = 0;
}
public static com.proto.render_server.ServerRequest parseFrom(
java.nio.ByteBuffer data)
throws com.google.protobuf.InvalidProtocolBufferException {
......@@ -404,62 +352,6 @@ public final class ServerRequest extends
return this;
}
/**
* <code>int32 width = 4;</code>
* @return The width.
*/
@java.lang.Override
public int getWidth() {
return instance.getWidth();
}
/**
* <code>int32 width = 4;</code>
* @param value The width to set.
* @return This builder for chaining.
*/
public Builder setWidth(int value) {
copyOnWrite();
instance.setWidth(value);
return this;
}
/**
* <code>int32 width = 4;</code>
* @return This builder for chaining.
*/
public Builder clearWidth() {
copyOnWrite();
instance.clearWidth();
return this;
}
/**
* <code>int32 height = 5;</code>
* @return The height.
*/
@java.lang.Override
public int getHeight() {
return instance.getHeight();
}
/**
* <code>int32 height = 5;</code>
* @param value The height to set.
* @return This builder for chaining.
*/
public Builder setHeight(int value) {
copyOnWrite();
instance.setHeight(value);
return this;
}
/**
* <code>int32 height = 5;</code>
* @return This builder for chaining.
*/
public Builder clearHeight() {
copyOnWrite();
instance.clearHeight();
return this;
}
// @@protoc_insertion_point(builder_scope:controller.ServerRequest)
}
@java.lang.Override
......@@ -479,12 +371,10 @@ public final class ServerRequest extends
"userid_",
"timeRequest_",
"deviceId_",
"width_",
"height_",
};
java.lang.String info =
"\u0000\u0005\u0000\u0000\u0001\u0005\u0005\u0000\u0000\u0000\u0001\u0004\u0002\t" +
"\u0003\u0208\u0004\u0004\u0005\u0004";
"\u0000\u0003\u0000\u0000\u0001\u0003\u0003\u0000\u0000\u0000\u0001\u0004\u0002\t" +
"\u0003\u0208";
return newMessageInfo(DEFAULT_INSTANCE, info, objects);
}
// fall through
......
......@@ -35,16 +35,4 @@ public interface ServerRequestOrBuilder extends
*/
com.google.protobuf.ByteString
getDeviceIdBytes();
/**
* <code>int32 width = 4;</code>
* @return The width.
*/
int getWidth();
/**
* <code>int32 height = 5;</code>
* @return The height.
*/
int getHeight();
}
......@@ -67,6 +67,32 @@ public final class ServerResponse extends
userid_ = 0;
}
public static final int COUNTING_FIELD_NUMBER = 3;
private int counting_;
/**
* <code>int32 counting = 3;</code>
* @return The counting.
*/
@java.lang.Override
public int getCounting() {
return counting_;
}
/**
* <code>int32 counting = 3;</code>
* @param value The counting to set.
*/
private void setCounting(int value) {
counting_ = value;
}
/**
* <code>int32 counting = 3;</code>
*/
private void clearCounting() {
counting_ = 0;
}
public static com.proto.render_server.ServerResponse parseFrom(
java.nio.ByteBuffer data)
throws com.google.protobuf.InvalidProtocolBufferException {
......@@ -218,6 +244,34 @@ public final class ServerResponse extends
return this;
}
/**
* <code>int32 counting = 3;</code>
* @return The counting.
*/
@java.lang.Override
public int getCounting() {
return instance.getCounting();
}
/**
* <code>int32 counting = 3;</code>
* @param value The counting to set.
* @return This builder for chaining.
*/
public Builder setCounting(int value) {
copyOnWrite();
instance.setCounting(value);
return this;
}
/**
* <code>int32 counting = 3;</code>
* @return This builder for chaining.
*/
public Builder clearCounting() {
copyOnWrite();
instance.clearCounting();
return this;
}
// @@protoc_insertion_point(builder_scope:controller.ServerResponse)
}
@java.lang.Override
......@@ -236,10 +290,11 @@ public final class ServerResponse extends
java.lang.Object[] objects = new java.lang.Object[] {
"heatmap_",
"userid_",
"counting_",
};
java.lang.String info =
"\u0000\u0002\u0000\u0000\u0001\u0002\u0002\u0000\u0000\u0000\u0001\n\u0002\u0004" +
"";
"\u0000\u0003\u0000\u0000\u0001\u0003\u0003\u0000\u0000\u0000\u0001\n\u0002\u0004" +
"\u0003\u0004";
return newMessageInfo(DEFAULT_INSTANCE, info, objects);
}
// fall through
......
......@@ -18,4 +18,10 @@ public interface ServerResponseOrBuilder extends
* @return The userid.
*/
int getUserid();
/**
* <code>int32 counting = 3;</code>
* @return The counting.
*/
int getCounting();
}
......@@ -19,7 +19,7 @@ message Box{
message TimeRequest{
Time start = 1;
Time end = 2;
int64 start = 1;
int64 end = 2;
}
......@@ -8,13 +8,12 @@ message ServerRequest{
int32 userid = 1;
common.TimeRequest timeRequest = 2;
string deviceId = 3;
int32 width = 4;
int32 height = 5;
}
message ServerResponse{
bytes heatmap = 1;
int32 userid = 2;
int32 counting = 3;
}
service RenderService{
......
package Ceph;
import Config.AppfileConfig;
import Config.SpringContext;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.S3ObjectInputStream;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageInputStream;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
public class CephHandler {
private String accessKey;
private String secretKey;
private String hostname;
private AppfileConfig appfileConfig;
public CephHandler(String accessKey, String secretKey, String hostname) {
this.accessKey = accessKey;
this.secretKey = secretKey;
this.hostname = hostname;
}
public CephHandler() {
appfileConfig = SpringContext.context.getBean("appfileConfig", AppfileConfig.class);
this.accessKey = appfileConfig.cephAccessKey;
this.secretKey = appfileConfig.cephPrivateKey;
this.hostname = appfileConfig.cephHostname;
}
public BufferedImage getBackground(String bucket, String key) throws IOException {
//get s3 stream Input
S3ObjectInputStream s3is = createConnect().getObject(bucket, key).getObjectContent();
//convert to ImageStreamInput
ImageInputStream is = ImageIO.createImageInputStream(s3is);
//read streamInput
return ImageIO.read(is);
}
private AmazonS3 createConnect() {
AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
AmazonS3 conn = new AmazonS3Client(credentials);
conn.setEndpoint(hostname);
return conn;
}
}
......@@ -38,12 +38,27 @@ public class AppfileConfig {
@Value("${mongo.database}")
public String mongoDatabase;
@Value("${mongo.collection}")
public String mongoCollection;
@Value("${mongo.box_collection}")
public String boxCollection;
@Value("${model.type}")
public String modelType;
@Value("${multiplier}")
public float multiplier;
@Value("${cephAccessKey}")
public String cephAccessKey;
@Value("${cephPrivateKey}")
public String cephPrivateKey;
@Value("${cephHostname}")
public String cephHostname;
@Value("${mongo.background_collection}")
public String backgroundCollection;
@Value("${mongo.track_collection}")
public String trackCollection;
}
package Controller;
import Ceph.CephHandler;
import com.google.protobuf.ByteString;
import com.proto.common.Box;
......@@ -27,11 +28,12 @@ import java.util.TimeZone;
public class RenderServerImpl extends RenderServiceGrpc.RenderServiceImplBase {
private MongoHandler mongoHandler;
private CephHandler cephHandler;
public RenderServerImpl() {
mongoHandler = new MongoHandler();
cephHandler = new CephHandler();
}
......@@ -41,19 +43,29 @@ public class RenderServerImpl extends RenderServiceGrpc.RenderServiceImplBase {
return df.parse(time);
}
private ByteString getResponse(ServerRequest request){
private ServerResponse getResponse(ServerRequest request) {
try {
//create response builder
ServerResponse.Builder serverResponseBuilder = ServerResponse.newBuilder();
//get start end
Date start = convertTime(request.getTimeRequest().getStart().getTime(),
request.getTimeRequest().getStart().getFormat());
Date end = convertTime(request.getTimeRequest().getEnd().getTime(),
request.getTimeRequest().getEnd().getFormat());
// Date start = convertTime(request.getTimeRequest().getStart().getTime(),
// request.getTimeRequest().getStart().getFormat());
// Date end = convertTime(request.getTimeRequest().getEnd().getTime(),
// request.getTimeRequest().getEnd().getFormat());
long start = request.getTimeRequest().getStart();
long end = request.getTimeRequest().getEnd();
//Create Heatmap
//find boxes in mongodb
List<Box> boxes = mongoHandler.findBoxes(request.getDeviceId(), start, end);
//find backgroundPath in mongodb
String[] bucket_path = mongoHandler.findBackgroundPath(request.getDeviceId(), start, end);
//get background from ceph
BufferedImage background = cephHandler.getBackground(bucket_path[0], bucket_path[1]);
//render heatmap
BufferedImage heatmap = Renderer.renderHeatMap(request.getWidth(), request.getHeight(), boxes);
BufferedImage heatmap = Renderer.renderHeatMap(background, boxes);
//convert heatmap
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(heatmap, "png", baos);
......@@ -61,12 +73,18 @@ public class RenderServerImpl extends RenderServiceGrpc.RenderServiceImplBase {
byte[] bytes = baos.toByteArray();
baos.close();
ByteBuffer buf = ByteBuffer.wrap(bytes);
ByteString byteString = ByteString.copyFrom(buf);
return byteString;
} catch (ParseException e) {
e.printStackTrace();
return null;
//add heatmap to builder
serverResponseBuilder.setHeatmap(byteString);
//Create counting
//find set of trackId in mongodb
// serverResponseBuilder.setCounting(mongoHandler.findTrackID(request.getDeviceId(), start, end).size());
//add userId to builder
serverResponseBuilder.setUserid(request.getUserid());
return serverResponseBuilder.build();
} catch (IOException e) {
e.printStackTrace();
return null;
......@@ -78,7 +96,7 @@ public class RenderServerImpl extends RenderServiceGrpc.RenderServiceImplBase {
StreamObserver<ServerRequest> streamObserver = new StreamObserver<ServerRequest>() {
@Override
public void onNext(ServerRequest value) {
responseObserver.onNext(ServerResponse.newBuilder().setHeatmap(getResponse(value)).setUserid(value.getUserid()).build());
responseObserver.onNext(getResponse(value));
}
@Override
......@@ -96,7 +114,7 @@ public class RenderServerImpl extends RenderServiceGrpc.RenderServiceImplBase {
@Override
public void getHeatmap(ServerRequest request, StreamObserver<ServerResponse> responseObserver) {
responseObserver.onNext(ServerResponse.newBuilder().setHeatmap(getResponse(request)).setUserid(request.getUserid()).build());
responseObserver.onNext(getResponse(request));
responseObserver.onCompleted();
}
......
......@@ -24,24 +24,18 @@ public class HeatMap {
private static final String SPECTRUMPIC = "colors.png";
private HashMap<Integer, List<Polygon>> polygonMap;
private int maxOccurance = 1;
private int maxXValue;
private int maxYValue;
private BufferedImage background;
AppfileConfig appfileConfig = SpringContext.context.getBean("appfileConfig", AppfileConfig.class);
public HeatMap(int width, int height) {
this.maxXValue = width;
this.maxYValue = height;
public HeatMap(BufferedImage background) {
this.background = background;
}
private HashMap<Integer, List<Box>> initMap(final List<Box> points) {
HashMap<Integer, List<Box>> map = new HashMap<Integer, List<Box>>();
final int pointSize = points.size();
points.forEach(point -> {
final int hash = getkey(point);
......@@ -62,10 +56,10 @@ public class HeatMap {
}
public BufferedImage createHeatMap(final float multiplier, List<Box> points) {
// System.out.println(background.getWidth()+" "+ background.getHeight());
HashMap<Integer, List<Box>> map = initMap(points);
BufferedImage heatMap = new BufferedImage(maxXValue, maxYValue, 6);
BufferedImage heatMap = new BufferedImage(background.getWidth(), background.getHeight(), 6);
paintInColor(heatMap, Color.white);
// paintInColor(heatMap,new Color(255,255,255,200));
......@@ -84,6 +78,7 @@ public class HeatMap {
Box currentPoint = currentPoints.get(0);
addImage(heatMap, null, opaque, currentPoint);
// addBox(heatMap, opaque, currentPoint);
}
......@@ -92,10 +87,10 @@ public class HeatMap {
remap(heatMap);
// final BufferedImage output = lvlMap;
// addImage(output, heatMap, 0.4f);
final BufferedImage output = background;
addImage(output, heatMap, 0.4f, null);
return heatMap;
return output;
}
private void remap(final BufferedImage heatMapBW) {
......@@ -165,7 +160,7 @@ public class HeatMap {
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opaque));
if (box != null) {
int radius = Math.max(box.getW() / 2, box.getH() / 2);
int radius = Math.max(box.getW()/2, box.getH()/2) ;
int x = box.getX();
int y = box.getY();
......@@ -174,14 +169,26 @@ public class HeatMap {
g2d.setPaint(new RadialGradientPaint(new Point.Double(x, y), radius, new float[]{0f, 1.0f},
new Color[]{new Color(0, 0, 0, color), new Color(0, 0, 0, 0)}));
// g2d.setPaint(new Color(0,0,0,255));
g2d.fillArc(x - radius, y - radius, 2 * radius, 2 * radius, 0, 360);
g2d.fillArc(x -radius, y -radius, 2 * radius, 2 * radius, 0, 360);
// System.out.println(x+" "+y);
} else {
g2d.drawImage(buff2, 0, 0, null);
g2d.dispose();
}
}
private void addBox(BufferedImage buff1, final float opaque, Box box){
Short color = (short) (255 * opaque);
Color c2 = new Color(0, 0, 0, color);
Graphics2D g2d = buff1.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER ,opaque));
g2d.setColor(c2);
// g2d.setPaint(gradient);
g2d.drawRect(box.getX(), box.getY(), box.getW(), box.getH());
g2d.dispose();
}
......
......@@ -13,10 +13,10 @@ public class Renderer {
static AppfileConfig appfileConfig = SpringContext.context.getBean("appfileConfig", AppfileConfig.class);
public static BufferedImage renderHeatMap(int width, int height, List<Box> boxes) {
public static BufferedImage renderHeatMap(BufferedImage background, List<Box> boxes) {
try {
HeatMap heatMap = new HeatMap(width, height);
HeatMap heatMap = new HeatMap(background);
// mat.release();
return heatMap.createHeatMap(appfileConfig.multiplier, boxes);
} catch (Exception e) {
......
......@@ -24,7 +24,7 @@ public class heatmapServiceImpl extends HeatmapServiceGrpc.HeatmapServiceImplBas
List<Box> boxes = request.getBoxesList();
//create heatmap
BufferedImage heatmap = Renderer.renderHeatMap(request.getWidth(), request.getHeight(), boxes);
BufferedImage heatmap = Renderer.renderHeatMap(null, boxes);
//convert bufferedImage to bytearray
......
......@@ -15,10 +15,7 @@ import entities.BBox;
import org.bson.Document;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
......@@ -28,61 +25,181 @@ public class MongoHandler {
public AppfileConfig appfileConfig;
public MongoHandler() {
appfileConfig = SpringContext.context.getBean("appfileConfig",AppfileConfig.class);
appfileConfig = SpringContext.context.getBean("appfileConfig", AppfileConfig.class);
initMongoClient();
}
public void initMongoClient(){
/**
* Create Mongo client
*/
public void initMongoClient() {
MongoCredential credential = MongoCredential.createCredential(appfileConfig.mongUser, "admin", appfileConfig.mongoPass.toCharArray());
mongoClient = new MongoClient(new ServerAddress(appfileConfig.mongoAddress, appfileConfig.mongoPort), Arrays.asList(credential));
database = mongoClient.getDatabase(appfileConfig.mongoDatabase);
}
public List<Box> findBoxes(String deviceId, Date start, Date end){
MongoCollection collection = database.getCollection(appfileConfig.mongoCollection);
/**
* Find the list of box with camID and time internal
* @param deviceId
* @param start
* @param end
* @return
*/
public List<Box> findBoxes(String deviceId, long start, long end) {
MongoCollection collection = database.getCollection(appfileConfig.boxCollection);
List<Box> boxes = new ArrayList<>();
//find match value
FindIterable<Document> findIterable = collection.find(Filters.and(
Filters.eq("DeviceID", deviceId),
Filters.eq("camId", deviceId),
Filters.gte("time", start),
Filters.lte("time", end)
));
MongoCursor<Document> cursor = findIterable.iterator();
//convert all find Document to BBox
while (cursor.hasNext()){
while (cursor.hasNext()) {
Document document = cursor.next();
List<Document> bboxes = (List<Document>) document.get("points");
boxes.addAll(bboxes.stream().map(convertDocumentToBox()).collect(Collectors.toList()));
List<List> bboxes = (List<List>) document.get("boxes");
boxes.addAll(bboxes.stream().map(document1 -> ListToBox(document1)).toList());
}
System.out.println(boxes.size());
// System.out.println(boxes.size());
return boxes;
}
/**
* Find the background of camID in the time internal
* @param deviceId
* @param start
* @param end
* @return
*/
public String[] findBackgroundPath(String deviceId, long start, long end) {
try {
//get Collecttion
MongoCollection collection = database.getCollection(appfileConfig.backgroundCollection);
//query and sort
FindIterable<Document> findIterable = collection.find(Filters.and(
Filters.eq("camId", deviceId),
Filters.gte("time", start),
Filters.lte("time", end)
)).sort(new BasicDBObject("time", 1));
//convert to list String[]
MongoCursor<Document> cursor = findIterable.iterator();
List<String[]> list = new ArrayList<>();
while (cursor.hasNext()) {
Document document = cursor.next();
if (!document.containsKey("path")) {
continue;
}
list.add(new String[]{
document.get("bucket").toString(),
document.get("path").toString()
});
}
//return the closest to center
return list.get(list.size() / 2);
} catch (Exception e) {
e.printStackTrace();
return new String[2];
}
}
public Set<Integer> findTrackID(String camId, long start, long end){
//get Collection
MongoCollection collection = database.getCollection(appfileConfig.trackCollection);
private Function<Document, Box> convertDocumentToBox(){
return document -> {
if (!document.containsKey("x")){
return null;
//query
FindIterable<Document> findIterable = collection.find(Filters.and(
Filters.eq("camId", camId),
Filters.gte("time", start),
Filters.lte("time", end)
));
MongoCursor<Document> cursor = findIterable.iterator();
Set<Integer> list_trackId = new HashSet<>();
//get list trackId
while (cursor.hasNext()){
Document document = cursor.next();
if (!document.containsKey("boxes")){
continue;
}
Box.Builder box = Box.newBuilder();
int x = (int) document.get("x");
int y = (int) document.get("y");
int w = (int) document.get("w");
int h = (int) document.get("h");
double score = (double) document.get("score");
box.setX(x);
box.setY(y);
box.setW(w);
box.setH(h);
box.setScore((float) score);
return box.build();
};
List<List> boxes = (List<List>) document.get("boxes");
list_trackId.addAll(boxes.stream().map(box -> (int) box.get(4)).collect(Collectors.toList()));
}
return list_trackId;
}
/**
* Convert x1y1x2y2 to xywh format
* @param x1
* @param y1
* @param x2
* @param y2
* @return
*/
private int[] x1y1x2y2_to_xywh(int x1, int y1, int x2, int y2){
return (new int[]{
(x2+x1)/2,
(y2+y1)/2,
x2-x1,
y2-y1
});
}
/**
* Convert list (from mongo) to box response
* @param box_value
* @return
*/
private Box ListToBox(List box_value){
Box.Builder box = Box.newBuilder();
//convert to xywh
int[] xywh = x1y1x2y2_to_xywh((int) box_value.get(0), (int) box_value.get(1), (int) box_value.get(2), (int) box_value.get(3));
box.setX(xywh[0]);
box.setY(xywh[1]);
box.setW(xywh[2]);
box.setH(xywh[3]);
box.setScore(0);
return box.build();
}
/**
* Convert document (from mongo) to box response
* @param document
* @return
*/
private Box DocumentToBox(Document document){
Box.Builder box = Box.newBuilder();
//get box
List box_value = (List) document;
//convert to xywh
int[] xywh = x1y1x2y2_to_xywh((int) box_value.get(0), (int) box_value.get(1), (int) box_value.get(2), (int) box_value.get(3));
box.setX(xywh[0]);
box.setY(xywh[1]);
box.setW(xywh[2]);
box.setH(xywh[3]);
box.setScore(0);
return box.build();
}
private Function<Document, Box> convertDocumentToBox() {
return document -> DocumentToBox(document);
}
}
......@@ -34,11 +34,13 @@ public class mongoServiceImpl extends MongoServiceGrpc.MongoServiceImplBase {
try{
//get start end
Date start = convertTime(request.getTimeRequest().getStart().getTime(),
request.getTimeRequest().getStart().getFormat());
Date end = convertTime(request.getTimeRequest().getEnd().getTime(),
request.getTimeRequest().getEnd().getFormat());
// Date start = convertTime(request.getTimeRequest().getStart().getTime(),
// request.getTimeRequest().getStart().getFormat());
// Date end = convertTime(request.getTimeRequest().getEnd().getTime(),
// request.getTimeRequest().getEnd().getFormat());
long start = request.getTimeRequest().getStart();
long end = request.getTimeRequest().getEnd();
//find boxes in mongodb
List<Box> boxes = mongoHandler.findBoxes(request.getDeviceId(), start, end);
......@@ -62,7 +64,7 @@ public class mongoServiceImpl extends MongoServiceGrpc.MongoServiceImplBase {
//complete service
responseObserver.onCompleted();
}catch(ParseException e){
}catch(Exception e){
System.out.println("Parse Exception");
e.printStackTrace();
}
......
......@@ -19,7 +19,7 @@ message Box{
message TimeRequest{
Time start = 1;
Time end = 2;
int64 start = 1;
int64 end = 2;
}
......@@ -8,13 +8,12 @@ message ServerRequest{
int32 userid = 1;
common.TimeRequest timeRequest = 2;
string deviceId = 3;
int32 width = 4;
int32 height = 5;
}
message ServerResponse{
bytes heatmap = 1;
int32 userid = 2;
int32 counting = 3;
}
service RenderService{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment