public async Task <IServiceResult> PostImagePublisher([FromBody] ImagePublisher imagePublisher) { try { _context.ImagePublishers.Add(imagePublisher); if (!await _unitOfWork.CompleteAsync()) { throw new SaveFailedException(nameof(imagePublisher)); } _logger.LogInformation($"Image {imagePublisher.Id} created."); return(new ServiceResult(payload: imagePublisher.UrlOnline)); } catch (Exception e) { _logger.LogError($"Can't create a image of game {imagePublisher.Id}. {e.Message}"); return(new ServiceResult(false, message: e.Message)); } }
//Inorder to send the image from both cameras,images are encoded/compressed first using JPG compression //then the compressed data is converted into a string in Base64 format. // Update is called once per frame void Update() { #if img2 //encoding part: StringBuilder imgToSend = new StringBuilder("", 500000); //Bottom Cam encoding: RenderTexture.active = bottomImage; imageToSend.ReadPixels(new Rect(0, 0, bottomImage.width, bottomImage.height), 0, 0); imageToSend.Apply(); Byte[] bottom_cam_image_jpg = ImageConversion.EncodeToJPG(imageToSend, 100); string bottom_cam_image_base64 = Convert.ToBase64String(bottom_cam_image_jpg); imgToSend.Append(bottom_cam_image_base64).Append("!"); //Front cam encoding RenderTexture.active = frontImage; imageToSend2.ReadPixels(new Rect(0, 0, frontImage.width, frontImage.height), 0, 0); imageToSend2.Apply(); Byte[] front_cam_image_jpg = ImageConversion.EncodeToJPG(imageToSend2, 100); //Byte[] front_cam_image_jpg =imageToSend2.GetRawTextureData<Color32>() string front_cam_image_base64 = Convert.ToBase64String(front_cam_image_jpg); imgToSend.Append(front_cam_image_base64).Append("!"); //sending the image data try { #if self2 imgMsg = new StringMsg(imgToSend.ToString()); obj.GetComponent <ROS_Initialize>().ros.Publish(ImagePublisher.GetMessageTopic(), imgMsg); Debug.Log("Sending to topic: " + ImagePublisher.GetMessageTopic()); //Debug.Log(imgMsg); #endif } catch (Exception e) { Debug.Log("Socket error" + e); } #endif }
// Update is called once per frame void Update() { #if img //encoding part: StringBuilder imgToSend = new StringBuilder("", 500000); //bottom cam encoding RenderTexture.active = depthImage; imageToSend.ReadPixels(new Rect(0, 0, depthImage.width, depthImage.height), 0, 0); imageToSend.Apply(); Byte[] depth_cam_image_jpg = ImageConversion.EncodeToJPG(imageToSend, 100); string depth_cam_image_base64 = Convert.ToBase64String(depth_cam_image_jpg); imgToSend.Append(depth_cam_image_base64).Append("!"); //front cam encoding RenderTexture.active = rgbImage; imageToSend2.ReadPixels(new Rect(0, 0, rgbImage.width, rgbImage.height), 0, 0); imageToSend2.Apply(); Byte[] rgb_cam_image_jpg = ImageConversion.EncodeToJPG(imageToSend2, 100); string rgb_cam_image_base64 = Convert.ToBase64String(rgb_cam_image_jpg); imgToSend.Append(rgb_cam_image_base64).Append("!"); //sending the image data try { #if self imgMsg = new StringMsg(imgToSend.ToString()); obj.GetComponent <ROSInitializerSLAM>().rosSLAM.Publish(ImagePublisher.GetMessageTopic(), imgMsg); //Debug.Log("Sending to topic: " + ImagePublisher.GetMessageTopic()); //Debug.Log(imgMsg); #endif } catch (Exception e) { Debug.Log("Socket error" + e); } #endif }
/* Update function sends images from the cameras attached to the AUV, to the control algorithm. In order * to maintain sufficient communication frequency, images cannot be sent in their entirety. Therefore, * edge detection is performed on the images, to detect meaningful data. * */ void Update() { // Examine whether Lock is required Vector3 CurRot = transform.parent.transform.rotation.eulerAngles; // if(CurRot.x > 180.0f) // CurRot.x -= 360.0f; // if(CurRot.y > 180.0f) // CurRot.y -= 360.0f; // if(CurRot.z > 180.0f) // CurRot.z -= 360.0f; // // CurRot *= 3.14f/180.0f; // Debug.Log (CurRot); #if UsePics // if(Input.GetKey(KeyCode.Space)) // firstSend = true; // Debug.Log (Time.deltaTime); if (!Lock) { Lock = true; firstSend = false; RenderTexture.active = bottomImage; imageToSend.ReadPixels(new Rect(0, 0, bottomImage.width, bottomImage.height), 0, 0); imageToSend.Apply(); StringBuilder pixelsToSend1 = new StringBuilder("", 500000); StringBuilder pixelsToSend2 = new StringBuilder("", 500000); StringBuilder pixelsToSend3 = new StringBuilder("", 500000); StringBuilder pixelsToSend4 = new StringBuilder("", 500000); StringBuilder pixelsToSend5 = new StringBuilder("", 500000); StringBuilder pixelsToSend6 = new StringBuilder("", 500000); int cnt = 0; Color32[] allPixels = imageToSend.GetPixels32(); //Bottom Image R Component for (int i = 0; i < bottomImage.height; i++) { bool prev = false; bool noChanges = true; for (int j = 0; j < bottomImage.width; j++) { Color32 currentVal = allPixels [(ImageHeight - 1 - i) * bottomImage.width + j]; if ((currentVal.r > colThreshHigh && currentVal.g < colThreshLow && currentVal.b < colThreshLow) != prev) { noChanges = false; prev = !prev; pixelsToSend1.Append(j).Append(":"); } } if (noChanges) { pixelsToSend1.Append("-1"); } pixelsToSend1.Append(">"); } //Bottom Image G Component for (int i = 0; i < bottomImage.height; i++) { bool prev = false; bool noChanges = true; for (int j = 0; j < bottomImage.width; j++) { Color32 currentVal = allPixels [(ImageHeight - 1 - i) * bottomImage.width + j]; if ((currentVal.g > colThreshHigh && currentVal.r < colThreshLow && currentVal.b < colThreshLow) != prev) { noChanges = false; prev = !prev; pixelsToSend2.Append(j).Append(":"); } } if (noChanges) { pixelsToSend2.Append("-1"); } pixelsToSend2.Append(">"); } //Bottom Image B Component for (int i = 0; i < bottomImage.height; i++) { bool prev = false; bool noChanges = true; for (int j = 0; j < bottomImage.width; j++) { Color32 currentVal = allPixels [(ImageHeight - 1 - i) * bottomImage.width + j]; if ((currentVal.b > colThreshHigh && currentVal.r < colThreshLow && currentVal.g < colThreshLow) != prev) { noChanges = false; prev = !prev; pixelsToSend3.Append(j).Append(":"); } } if (noChanges) { pixelsToSend3.Append("-1"); } pixelsToSend3.Append(">"); } // for(int i=0; i<bottomImage.width*bottomImage.height; i++) { // Color32 pixel = allPixels [i]; // if (pixel.r > colThresh) { // pixelsToSend1.Append(cnt.ToString ("00000")).Append(":"); // } // cnt++; // } cnt = 0; RenderTexture.active = frontImage; imageToSend2.ReadPixels(new Rect(0, 0, frontImage.width, frontImage.height), 0, 0); imageToSend2.Apply(); allPixels = imageToSend2.GetPixels32(); //Front Image R Component for (int i = 0; i < frontImage.height; i++) { bool prev = false; bool noChanges = true; for (int j = 0; j < frontImage.width; j++) { Color32 currentVal = allPixels [(ImageHeight - 1 - i) * frontImage.width + j]; if ((currentVal.r > colThreshHigh && currentVal.g < colThreshLow && currentVal.b < colThreshLow) != prev) { noChanges = false; prev = !prev; pixelsToSend4.Append(j).Append(":"); } } if (noChanges) { pixelsToSend4.Append("-1"); } pixelsToSend4.Append(">"); } //Front Image G Component for (int i = 0; i < frontImage.height; i++) { bool prev = false; bool noChanges = true; for (int j = 0; j < frontImage.width; j++) { Color32 currentVal = allPixels [(ImageHeight - 1 - i) * frontImage.width + j]; if ((currentVal.g > colThreshHigh && currentVal.r < colThreshLow && currentVal.b < colThreshLow) != prev) { noChanges = false; prev = !prev; pixelsToSend5.Append(j).Append(":"); } } if (noChanges) { pixelsToSend5.Append("-1"); } pixelsToSend5.Append(">"); } //Front Image B Component for (int i = 0; i < frontImage.height; i++) { bool prev = false; bool noChanges = true; for (int j = 0; j < frontImage.width; j++) { Color32 currentVal = allPixels [(ImageHeight - 1 - i) * frontImage.width + j]; if ((currentVal.b > colThreshHigh && currentVal.r < colThreshLow && currentVal.g < colThreshLow) != prev) { noChanges = false; prev = !prev; pixelsToSend6.Append(j).Append(":"); } } if (noChanges) { pixelsToSend6.Append("-1"); } pixelsToSend6.Append(">"); } // for(int i=0; i<frontImage.width*frontImage.height; i++) { // Color32 pixel = allPixels [i]; // if (pixel.r > colThresh) { // pixelsToSend2.Append(cnt.ToString ("00000")).Append(":"); // } // cnt++; // } // Debug.Log(pixelsToSend1.Length + " " + pixelsToSend1); // Debug.Log("Bottom Cam" + pixelsToSend1.Length + " " + pixelsToSend1.ToString()); pixelsToSend1.Append("!").Append(pixelsToSend2).Append("!").Append(pixelsToSend3).Append("!") .Append(pixelsToSend4).Append("!").Append(pixelsToSend5).Append("!").Append(pixelsToSend6); // pixelsToSend1.Append ("!").Append (pixelsToSend3); try { #if notSelf // theWriter.WriteLine(pixelsToSend1.Length.ToString("000000")); // theWriter.Flush(); // pixelsToSend1.Insert(0, pixelsToSend1.Length.ToString("00000000") + " "); imgMsg = new StringMsg(pixelsToSend1.ToString()); obj.GetComponent <ROS_Initialize> ().ros.Publish(ImagePublisher.GetMessageTopic(), imgMsg); // theWriter.WriteLine(pixelsToSend1);//.ToString().Substring(0, 2000));//.ToString().Substring(0, 1022)); // theWriter.Flush(); // theWriter.WriteLine(pixelsToSend1.ToString().Substring(0, 2048)); // theWriter.WriteLine(pixelsToSend1); // Debug.Log(Encoding.ASCII.GetBytes(pixelsToSend1.ToString()).Length); // theStream.Write(Encoding.ASCII.GetBytes(pixelsToSend1.ToString()), 0, Encoding.ASCII.GetBytes(pixelsToSend1.ToString()).Length); // theWriter.Flush(); #endif // Debug.Log(pixelsToSend1.Length + " " + pixelsToSend1); // Debug.Log(pixelsToSend2.Length + " " + pixelsToSend2); // Debug.Log("Front Cam" + pixelsToSend2.Length + " " + pixelsToSend2.ToString()); // Debug.Log(pixelsToSend3.Length + " " + pixelsToSend3); // Debug.Log(pixelsToSend3.Length + " " + pixelsToSend3.ToString()); } catch (Exception e) { Debug.Log("Socket error" + e); } // pixelsToSend2 = ""; cnt = 0; // foreach (Color32 pixel in imageToSend.GetPixels32()) { // if (pixel.r > colThresh) { //// pixelsToSend2 = pixelsToSend2 + " " + (cnt / bottomImage.width).ToString ("000") + " " + //// (cnt % bottomImage.height).ToString ("000") + ":"; // Debug.Log (cnt); // } // cnt++; // } // try { // #if notSelf // theWriter.WriteLine(imageToSend.GetPixels32()[0]); // theWriter.Flush(); // theWriter.WriteLine(":"); // theWriter.Flush(); // Debug.Log("afaeeagaggaegggggggggggggggggg " + imageToSend.GetPixels32()[0]); // // #endif // //// Debug.Log(pixelsToSend1.Length + pixelsToSend1); // } catch (Exception e) { // Debug.Log ("Socket error" + e); // } } #endif }